How to use a for loop in C language to iterate through a sample.

In C language, you can use a for loop to iterate through a sample. Here is an example:

#include <stdio.h>

int main() {
    int samples[] = {1, 2, 3, 4, 5};
    int length = sizeof(samples) / sizeof(samples[0]);
    
    // 使用for循环遍历数组
    for (int i = 0; i < length; i++) {
        printf("%d\n", samples[i]);
    }

    return 0;
}

The code above declares an integer array called samples, then calculates the length of the array by using the array’s length property. It then loops through the array using a for loop, incrementing i from 0 to the length of the array minus 1, to access each element in the array and print it.

By running the above code, the output result is:

1
2
3
4
5

The sample has been fully navigated through.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds