7.6 Iteration Statements

An iteration statement, or loop, repeatedly executes a statement, known as the loop body, until the controlling expression is false (0). The control expression must have a scalar type.

The while statement evaluates the control expression before executing the loop body (see Section 7.6.1).

The do statement evaluates the control expression after executing the loop body; at least one execution of the loop body is guaranteed (see Section 7.6.2).

The for statement executes the loop body based on the evaluation of the second of three expressions (see Section 7.6.3).

7.6.1 The while Statement

The while statement evaluates a control expression before each execution of the loop body. If the control expression is true (nonzero), the loop body is executed. If the control expression is false (0), the while statement terminates. The while statement has the following syntax:

while ( expression )

   statement

Consider the following while statement:

n = 0;
while (n < 10)
   {
      a[n] = n;
      n++;
   }

This statement tests the value of n ; if n is less than 10, it assigns n to the nth element of the array a and then increments n . The control expression (in parentheses) is then evaluated; if true (nonzero), the loop body is executed again; if false (0), the while statement terminates. If the statement n++ were missing from the loop body, this while statement would never terminate. If the statement n = 0 were replaced by the statement n = 10 , the control expression is initially false (0), and the loop body is never executed.

7.6.2 The do Statement

The do statement evaluates the control expression after each execution of the loop body. The do statement has the following syntax:

do

   statement
   while ( expression ) ;

The loop body is executed at least once. The control expression is evaluated after each execution of the loop body. If the control expression is true (nonzero), the statement is executed again. If the control expression is false (0), the do statement terminates.

7.6.3 The for Statement

The for statement evaluates three expressions and executes the loop body until the second controlling expression evaluates to false (0). The for statement is useful for executing a loop body a specified number of times. The for statement has the following syntax:

for ( expression-1(opt) ;

   expression-2(opt) ; expression-3(opt))
   statement

The for statement is equivalent to the following code:

expression-1;
while ( expression-2 )

   {
   statement
   expression-3 ;
   }

The for statement executes the loop body zero or more times. Semicolons (;) are used to separate the control expressions. A for statement executes the following steps:

  1. expression-1 is evaluated once before the first iteration of the loop. This expression usually specifies the initial values for variables used in the loop.

  2. expression-2 is any scalar expression that determines whether to terminate the loop. expression-2 is evaluated before each loop iteration. If the expression is true (nonzero), the loop body is executed. If the expression is false (0), execution of the for statement terminates.

  3. expression-3 is evaluated after each iteration.

  4. The for statement executes until expression-2 is false (0), or until a jump statement, such as break or goto , terminates execution of the loop.

Any of the three expressions in a for loop can be omitted:


Previous Page | Next Page | Table of Contents | Index