for loop

< c‎ | language

Executes a loop.

Used as a shorter equivalent of while loop.


[edit] Syntax

for ( init_clause ; cond_expression ; iteration_expression ) loop_statement

[edit] Explanation

Behaves as follows:

  • init_clause may be an expression or a declaration
  • If it is an expression, it is evaluated once, before the first evaluation of cond_expression and its result is discarded.
  • (C99) If it is a declaration, it is in scope in the entire loop body, including the remainder of init_clause, the entire cond_expression, the entire iteration_expression and the entire loop_statement. Only auto and register storage classes are allowed for the variables declared in this declaration.
  • cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
  • iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.

init_clause, cond_expression, and iteration_expression are all optional:

for(;;) {
   printf("endless loop!");

loop_statement is not optional, but it may be a null statement:

for(int n = 0; n < 10; ++n, printf("%d\n", n))
    ; // null statement

If the execution of the loop needs to be terminated at some point, a break statement can be used anywhere within the loop_statement.

The continue statement used anywhere within the loop_statement transfers control to iteration_expression.

As an extension to the as-if rule, a loop with no side effects (I/O, volatile accesses, or atomic synchronizations) in any part of its cond_expression, iteration_expression or loop_statement may be optimized out even if it is an endless loop. The only exceptions are the loops where cond_expression is omitted or is a constant expression; for(;;) is always an endless loop.

As with all other selection and iteration statements, the for statement establishes block scope: any identifier introduced in the init_clause, cond_expression, or iteration_expression goes out of scope after the loop_statement.

(since C99)

[edit] Keywords


[edit] Notes

The expression statement used as loop_statement establishes its own block scope, distinct from the scope of init_clause, unlike in C++:

for (int i = 0; ; ) {
    long i = 1;   // valid C, invalid C++
    // ...

It is possible to enter the body of a loop using goto. In this case, init_clause and cond_expression are not executed.

[edit] Example

#include <stdio.h>
#include <stdlib.h>
enum { SIZE = 8 };
int main(void)
    int array[SIZE];
    for(size_t i = 0 ; i < SIZE; ++i)
        array [i] = rand() % 2;
    printf("Array filled!\n");
    for (size_t i = 0; i < SIZE; ++i)
        printf("%d ", array[i]);

Possible output:

Array filled!
1 0 1 1 1 1 0 0

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • The for statement (p: 151)
  • C99 standard (ISO/IEC 9899:1999):
  • The for statement (p: 136)
  • C89/C90 standard (ISO/IEC 9899:1990):

[edit] See also

C++ documentation for for loop