spirito-buono
4/26/2018 - 7:32 AM

For loops

For loops in C are straightforward. They supply the ability to create a loop - a code block that runs multiple times. For loops require an iterator variable, usually notated as i.

For loops give the following functionality:

Initialize the iterator variable using an initial value Check if the iterator has reached its final value Increases the iterator

int i;
for (i = 0; i < 10; i++) {
    printf("%d\n", i);
}

int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
int i;

for (i = 0; i < 10; i++) {
    sum += array[i];
}

/* sum now contains a[0] + a[1] + ... + a[9] */
printf("Sum of the array is %d\n", sum);