Skip to main content
A for loop in C is an entry-controlled iteration statement that consolidates the initialization, condition testing, and state mutation of a loop into a single syntactic construct. It dictates the repeated execution of a statement or block of statements as long as a specified scalar expression evaluates to a non-zero value.
for (initialization; test_expression; update_expression) {
    // loop_body
}

Structural Components

The loop header consists of three distinct clauses separated by semicolons:
  • initialization: Evaluated exactly once before the loop begins. According to the C standard, this clause is either an expression or a declaration. It is not strictly an expression. If it is a declaration (introduced in C99, e.g., int i = 0), the declared variables possess block scope, limiting their lifetime and visibility strictly to the loop. If it is an expression, its result is discarded as a void expression.
  • test_expression: A scalar expression evaluated prior to every iteration. If it evaluates to a non-zero value (true), control flow enters the loop_body. If it evaluates to zero (false), the loop terminates, and control flow transfers to the statement immediately following the loop construct.
  • update_expression: Evaluated at the end of each iteration. It executes immediately after the loop_body completes normally, or immediately after a continue statement is encountered within the body (which skips the remainder of the loop body and jumps directly to this expression). It is evaluated as a void expression, meaning its return value is discarded. Its primary purpose is to mutate the loop control variable(s).

Execution Flow

The internal mechanics of the for loop follow a strict sequence:
  1. Execute the initialization clause.
  2. Evaluate the test_expression.
  3. If the test_expression yields 0, terminate the loop.
  4. Execute the loop_body. (If a continue statement is executed, control flow jumps directly to step 5).
  5. Evaluate the update_expression.
  6. Return to step 2.

Technical Characteristics

Omitted Clauses Any or all of the three clauses in the loop header may be omitted, though the semicolons must remain. Omitting the test_expression implicitly replaces it with a non-zero constant, creating an unconditional (infinite) loop.
for (;;) {
    // Executes indefinitely unless interrupted by break, goto, or return
}
Declarator Lists vs. The Comma Operator When managing multiple control variables, it is critical to distinguish between a declarator list punctuator and the comma operator.
for (int i = 0, j = 10; i < j; i++, j--) {
    // loop_body
}
In the initialization clause (int i = 0, j = 10), the comma acts as a punctuator separating a list of declarators within a single declaration; it is not the comma operator. Conversely, the comma used in the update_expression (i++, j--) is the true comma operator, which guarantees left-to-right evaluation and introduces a sequence point between its left and right operands. Sequence Points The C standard defines strict sequence points within the for loop execution. A sequence point exists immediately after the evaluation of the initialization clause, after the test_expression, and after the update_expression. This guarantees that all side effects (such as variable increments or memory writes) from one phase are fully resolved before the next phase of the loop’s execution cycle begins.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More