Looping through an array with C

In C programming, arrays are often used in conjunction with loops for various operations on array elements. There are different types of loops in C, namely the for loop, while loop, and do-while loop, each of which can be used for iterating over array elements to perform different tasks.

C Array using for Loop

The for loop is commonly used when you know the number of iterations you need, making it suitable for iterating over arrays with a known size.

Syntax:
for (initialization; condition; increment) { // code block }

The initialization part is executed once before the loop starts. The condition part is evaluated before each iteration of the loop. If the condition is true, the code block is executed. After the code block is executed, the increment part is executed. The loop continues iterating until the condition is false.

Example:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int sum = 0; for (int i = 0; i < 5; i++) { sum += numbers[i]; } printf("Sum of elements in the array: %d\n", sum); return 0; }

In this example, we use a for loop to iterate through the elements of the numbers array and calculate their sum.

C Array using while Loop

The while loop is suitable when you want to iterate over array elements based on a condition.

Syntax:
while (condition) { // code block }

The condition part is evaluated before each iteration of the loop. If the condition is true, the code block is executed. The loop continues iterating until the condition is false.

Example:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int product = 1; int i = 0; while (i < 5) { product *= numbers[i]; i++; } printf("Product of elements in the array: %d\n", product); return 0; }

In this example, we use a while loop to calculate the product of elements in the numbers array. The loop continues until the condition i < 5 is no longer true.

C Array using do-while Loop

The do-while loop is similar to the while loop but guarantees that the loop body is executed at least once, as the condition is checked after the loop body.

Syntax:
do { // code block } while (condition);

The condition part is evaluated after the code block is executed. If the condition is true, the loop iterates again. The loop continues iterating until the condition is false.

Example:
#include <stdio.h> int main() { int numbers[] = {1, 2, 3, 4, 5}; int i = 0; do { printf("%d ", numbers[i]); i++; } while (i < 5); printf("\n"); return 0; }

In this example, we use a do-while loop to print the elements of the numbers array. The loop body is executed at least once because the condition is checked after the loop body.

Nested loop

A nested loop is a loop inside another loop. Nested loops can be used to iterate over arrays in multiple dimensions.

Example:
int myArray[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Print the elements of the array using a nested for loop for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", myArray[i][j]); } printf("\n"); }
Output: 1 2 3 4 5 6

Conclusion

Arrays are collections of elements that can be efficiently iterated using various loop types, including the for, while, and do-while loops. These loops allow programmers to perform tasks such as summing elements, finding maximum or minimum values, or processing array data based on specific conditions, providing flexibility and control when working with arrays in C.