next up previous contents
Next: 6.5 Break Up: 6 Control Flow Previous: 6.3 While

6.4 For

The syntax of a for loop is the following:

for ( expr-1 ; expr-2 ; expr-3 )
statement

This is equivalent to the following construct using while:

expr-1 ;
while ( expr-2 ) {
statement
expr-3 ;
}

Typically, expr-1 is an assignment, expr-2 is a relational expression, and expr-3 is an increment or decrement of some manner. For example, the following code counts from 0 to 99, printing each number along the way:

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



Fred G. Martin
Fri Mar 29 17:44:15 EST 1996