|
Example
i = 1;
while (i < 21)
type (i++);
types the value of i 20 times.
for ( [ expression1 ]; [ expression2 ]; [ expression3 ]; )
statement
First, expression1 is implemented as the starting condition. While expression2 is true, the statement is executed, and expression3 is evaluated. The loop repeats until expression2 is found to be false. This statement is equivalent to:
expression1 # state initial condition
while (expression2) { # while this is true
statement # perform this statement and
expression3 # evaluate this expression
}
Example
The for loop below performs the same function as the while loop above.
for (i=1; i<21; i++)
type (i);
Note that if expression2 is missing, it is always considered true, so that
for (i=1;;i++)
type (i);
is an infinite loop.
do
statement
while ( expression );
The statement is executed and then the expression is evaluated. If the expression is true, then the cycle is repeated. This statement differs from the while and for statements in that the expression is evaluated at the end. Therefore, the loop is always executed at least once. |
|