The for statement lets you repeat a statement a specified number of times. It consists of three expressions:
An initializing expression, which is evaluated when the loop begins
A test expression, which is evaluated before each iteration of the loop
A modifying expression, which is evaluated at the end of each iteration of the loop
These expressions are enclosed in parentheses and followed by the loop body—the statement the loop is to execute. Each expression in the parentheses can be any legal C statement.
The for statement works as follows:
1.The initializing expression is evaluated.
2.As long as the test expression evaluates to a nonzero value, the loop body is executed. When the test expression becomes 0, control passes to the statement following the loop body.
3.At the end of each iteration of the loop, the modifying expression is evaluated.
You can use a break, goto, or return statement to exit a for loop early. Use the continue statement to terminate an iteration without exiting the for loop. The continue statement passes control to the next iteration of the loop.
The following example illustrates for:
for( counter = 0; counter < 100; counter++ )
{
x[counter] = 0; /* Set every array element to zero */
}