HP C/iX Reference Manual (31506-90011)
Chapter 6 89
Statements
Iteration Statements
Iteration Statements
You use iteration statements to force a program to execute a statement repeatedly. The
executed statement is called the loop body. Loops execute until the value of a controlling
expression is 0. The controlling expression may be of any scalar type.
C has several iteration statements: while, do-while, and for. The main difference
between these statements is the point at which each loop tests for the exit condition. Refer
to the goto, continue, and break statements for ways to exit a loop without reaching its
end or meeting its exit condition.
Syntax
iteration-statement
::=
while (
expression
)
statement
do
statement
while (
expression
);
for (
[expression1]
;
[expression2]
;
[expression3]
)
statement
Examples
These three loops all accomplish the same thing (they assign i to a[i] for i from 0 to 4):
The while loop
i=0;
while (i < 5)
{
a[i] = i;
i++;
}
The do-while loop
i=0;
do
{
a[i] = i;
i++;
} while (i < 5);
The for loop
for(i=0;i<5;i++)
{
a[i] = i;
}