Cfor Loop

for Loop

The for loop is C's classic tool for repeating a block of code a known (or knowable) number of times. It packs initialization, the loop condition, and the update step into one compact header, which makes counting loops easy to read at a glance.
Classic Three-Part Syntax
A for loop has three parts separated by semicolons: initialization (runs once, before the loop starts), condition (checked before every iteration; the loop stops as soon as it is false), and update (runs after every iteration's body).

C
#include <stdio.h>

int main(void) {
    for (int i = 0; i < 5; i++) {
        printf("i = %d\n", i);
    }
    // Output: i = 0, i = 1, i = 2, i = 3, i = 4

    return 0;
}
Declaring the Loop Variable Inside the Header (C99+)
Declaring i directly inside the for header, as above, is a C99 feature. It scopes the variable to the loop itself — it does not exist before the loop and is not accessible after it ends. Older C89-only compilers require declaring the variable beforehand.

C
// C99 and later: i is scoped to the loop only
for (int i = 0; i < 3; i++) {
    printf("%d\n", i);
}
// i is NOT visible here

// C89-compatible style: declare the variable outside first
int j;
for (j = 0; j < 3; j++) {
    printf("%d\n", j);
}
// j IS still visible here (and holds 3 after the loop ends)
Note
Compile with -std=c99 or later (the modern default for most compilers) to use in-header declarations. If you ever see a "variable declaration not allowed here" error, it usually means the code is being compiled in strict C89 mode.
Multiple Init/Update Expressions With the Comma Operator
Each of the three parts of a for header can actually contain more than one expression, separated by the comma operator. This is commonly used to track two related counters at once, such as indices moving toward each other.

C
#include <stdio.h>

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = 5;

    // i counts up from the front, j counts down from the back
    for (int i = 0, j = n - 1; i < j; i++, j--) {
        printf("swap positions %d and %d\n", i, j);
    }

    return 0;
}
The Infinite for(;;) Loop
Any of the three parts of a for header can be left empty. Omitting the condition entirely makes it always true, so for (;;) loops forever unless something inside the body explicitly stops it (typically with break or return).

C
#include <stdio.h>

int main(void) {
    int count = 0;

    for (;;) {
        printf("tick %d\n", count);
        count++;
        if (count >= 3) {
            break; // the only way this loop ends
        }
    }

    return 0;
}
Tip
for (;;) and while (1) are functionally equivalent ways to write an intentional infinite loop in C; which one you use is mostly a matter of team convention.
Nested for Loops (Preview)
Just like if statements, for loops can be nested inside each other — an outer loop's body can contain a complete inner loop. This is the standard way to walk a 2D grid, print patterns, or compare every pair of elements in an array. The dedicated nested-loops page covers this in depth, including performance considerations.

C
// Preview: a nested for loop, covered in full on the Nested Loops page
for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 3; col++) {
        printf("(%d,%d) ", row, col);
    }
    printf("\n");
}
Key Points
  • A for header has three parts: initialization; condition; update -- each separated by a semicolon.

  • The condition is checked before every iteration; the loop stops the moment it becomes false.

  • C99 and later allow declaring the loop variable inside the header, scoping it to the loop.

  • The comma operator lets you initialize/update more than one variable per header.

  • Any part of the header can be omitted; for(;;) is a common idiom for an intentional infinite loop.