The for expression continues to evaluate the given statements until
the completion criteria is met. Formally,
for ( initialization expr; completion expr; increment ) {
statement;
}
Any number of statements can be in the initialization or increment steps as
long as they are simple and separated by commas. The completion statement
must result in a boolean value that determines when to end the loop. Example:
/*
* loop from 1 to 10
*/
int i;
printf ( "i =" );
for ( i = 0; i < 11; i++ ) {
printf ( " %d", i );
}
printf ( "\n" );
produces
i = 0 1 2 3 4 5 6 7 8 9 10
And a slightly more complex loop
/*
* loop from 1 to 10
*/
int i, j;
for ( i = 0, j = 1; j < 11; i++, j++ ) {
printf ( "i = %d, j = %d\n", i, j );
}
produces
i = 0, j = 1
i = 1, j = 2
i = 2, j = 3
i = 3, j = 4
i = 4, j = 5
i = 5, j = 6
i = 6, j = 7
i = 7, j = 8
i = 8, j = 9
i = 9, j = 10