HP C A.06.05 Reference Manual
Statements
for
Chapter 6162
4. After
statement
is executed,
expression3
is evaluated. Then the statement loops back to
test
expression2
again.
for Loop Processing
The for loop continues to execute until
expression2
evaluates to 0 (false), or until a branch
statement, such as a break or goto, interrupts loop execution.
If the loop body executes a continue statement, control passes to
expression3
. Except for
the special processing of the continue statement, the for statement is equivalent to the
following:
expression1;
while (
expression2
) {
statement
expression3;
}
You may omit any of the three expressions. If
expression2
(the controlling expression) is
omitted, it is taken to be a nonzero constant.
for versus while Loops
Note that for loops can be written as while loops, and vice versa. For example, the for loop
for (j = 0; j < 10; j++)
{
do_something();
}
is the same as the following while loop:
j = 0;
while (j<10)
{
do_something();
j++;
}
Example
/* Program name is "for_example". The following computes a
* permutation that is, P(n,m) = n!/(n-m)! using for
* loops to compute n! and (n-m)!
*/
#include <stdio.h>