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).
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.
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.
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:
for
statement
terminates.
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:
while
loop equivalent
becomes while(1)
. This is an infinite loop. For
example:
for (i = 0; ;i++) statement;
Infinite loops can be terminated with a break
,
return
, or goto
statement within the
loop body.
for
statement, the omitted
expression is evaluated as a void
expression and
is effectively dropped from the expansion. For example:
n = 1; for ( ; n < 10; n++) func(n);
In this example, n
is initialized before the
for
statement is executed.