next up previous contents
Next: 3.1.10.3 The break Statement Up: 3.1.10 Controlling the Program Previous: 3.1.10.1 Conditional Expressions

3.1.10.2 Loop Expressions

Iterations can be expressed as a for, while, or do statement. Each of these statements executes the body of the loop (expression_list) repeatedly until the result of the conditional expression boolean_expression becomes false or the loop is explicitly discontinued by using a break or return statement.

The while statement starts by evaluating the condition and executes the controlled statements in the expression_list if the result of boolean_expression is true. This sequence is repeated until the condition becomes false.

while ( boolean_expression ) {
   expression_list
}

The do statement works much like the while statement, but starts with executing the expression_list and evaluates the condition afterwards.

do {
   expression_list
} while  ( boolean_expression )

In short words, the controlled statements of a while loop are executed zero to infinitely times while the expression_list of a do statement is executed at least once.

A combination of the behavior of these two loop expressions can be achieved by using the for statement. It starts by executing the first expression within the parentheses which serves as initialization statement for the loop. Then the actual loop with each iteration consisting of three steps is executed. The first step is to evaluate the boolean_expression. If the result of this condition is false the program evaluation continues at the first statement after the for statement. Otherwise the controlled statement (expression_list) is executed followed by the evaluation of the third expression within the parentheses at the beginning of the for statement.

for ( expression, boolean_expression , expression ) {
   expression_list
}


next up previous contents
Next: 3.1.10.3 The break Statement Up: 3.1.10 Controlling the Program Previous: 3.1.10.1 Conditional Expressions
Robert Mlekus
1999-11-14