C – Iterate over Array using While Loop

To iterate over Array using While Loop, start with index=0, and increment the index until the end of array, and during each iteration inside while loop, access the element using index.

Refer C While Loop tutorial.

Examples

In the following example, we take an integer array arr, and loop over the elements of this array using While loop.

main.c

#include <stdio.h>

int main() {
    int arr[] = {2, 4, 6, 8};
    int arrLen = sizeof arr / sizeof arr[0];
    
    int i = 0;
    while (i < arrLen) {
        printf("%d\n", arr[i]);
        i++;
    }
    return 0;
}

Output

2
4
6
8
Program ended with exit code: 0
ADVERTISEMENT

Conclusion

In this C Tutorial, we learned how to iterate over array using while loop.