{ }codelper

Reference › C

C for Loop

Use for when you know how many times to loop. Inside the parentheses you write the start (initializer), the condition to keep going (condition), and what to do at the end of each pass (update), on one line separated by two semicolons.

for (initializer; condition; update) {
    statements to repeat
}

The basic form and the order it runs in

The order is this: run the initializer exactly once → if the condition is true, run the body → the update → the condition again → … The loop ends when the condition becomes false. If it is false from the start, the body never runs at all.

#include <stdio.h> int main(void) {    int n;    scanf("%d", &n);    for (int i = 1; i <= n; i++) {        printf("%d x %d = %d\n", i, i, i * i);    }    return 0;}
Input
4
Output
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16

Edit and run it →

The i created in the initializer can be used only inside this for. A loop that runs n times can be written with i = 1; i <= n or with i = 0; i < n. Array indexes start at 0, so the second form is used more often.

Counting down and counting by steps

The update doesn't have to be i++. You can count down with i-- or move several steps at a time with i += 5. When you count down, you also have to flip the comparison in the condition.

#include <stdio.h> int main(void) {    for (int i = 3; i >= 1; i--) {        printf("%d\n", i);    }    printf("go\n");    for (int i = 0; i <= 20; i += 5) {        printf("%d ", i);    }    printf("\n");    return 0;}
Output
3
2
1
go
0 5 10 15 20 

Edit and run it →

Nested for loops

When you put a for inside a for, the inner loop runs from start to finish for each single pass of the outer one. Use this for things with rows and columns, such as tables and grids. Give the two loops different variable names. With the same name, the inner loop cannot use the outer variable.

#include <stdio.h> int main(void) {    for (int row = 1; row <= 3; row++) {        for (int col = 1; col <= 4; col++) {            printf("%3d", row * col);        }        printf("\n");    }    return 0;}
Output
  1  2  3  4
  2  4  6  8
  3  6  9 12

Edit and run it →

After the inner for has printed a whole row, the outer loop ends the line with \n. The body runs 3 x 4 = 12 times in total.

break and continue

break ends the loop right there. continue skips only the rest of the current pass, goes to the update, and carries on with the next pass. Both apply only to the innermost loop around them.

#include <stdio.h> int main(void) {    int sum = 0;    for (int i = 1; i <= 100; i++) {        if (i % 3 == 0) continue;        if (sum + i > 20) break;        sum += i;        printf("+%d = %d\n", i, sum);    }    return 0;}
Output
+1 = 1
+2 = 3
+4 = 7
+5 = 12
+7 = 19

Edit and run it →

3 and 6 were skipped by continue, and adding 8 would go over 20, so break ended the loop. All three parts can also be left empty, so for (;;) is an infinite loop that runs until it reaches a break.

Common mistakes

Putting a semicolon after the parentheses

#include <stdio.h>

int main(void) {
    int i;
    for (i = 0; i < 3; i++);
    {
        printf("hello %d\n", i);
    }
    return 0;
}

→ hello 3 once, not three times

The semicolon becomes an "empty statement", and only that is repeated three times. The block in braces runs once after the loop has finished. Remove the ; after for (...).

Writing <= instead of < and going one element too far

int a[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i <= 5; i++) {   /* a[5] does not exist */
    sum += a[i];
}

→ This reads an element outside the array, which is undefined behavior. The sum may come out as a strange value

Starting from 0, use i < count. Starting from 1, use i <= count. An array with 5 elements has indexes 0 to 4, so write i < 5.

Using a variable created in the for outside it

for (int i = 0; i < 10; i++) {
    if (i * i > 50) break;
}
printf("%d\n", i);   /* i does not exist here */

→ It does not compile

A variable declared in the initializer is gone when the for ends. If you still need its value after the loop, declare int i; before the for and write for (i = 0; ...).

Practice problems

See also