HP C A.06.05 Reference Manual
Statements
Looping Statements
Chapter 6 153
Looping Statements
Syntax
while (
expression
)
statement
do
statement
while (
expression
);
for ([
expression1
] ; [
expression2
]; [
expression3
])
statement
Description
Use looping statements to force a program to repeatedly execute a statement. The executed
statement is called the loop body. Loops execute until the control is satisfied. The controlling
expression may be any scalar data type.
C has several looping 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
loop exit tests.
Examples
The following loops all accomplish the same thing (they assign i to a[i] for i from 0 to 4):
i = 0;
while (i < 5)
{
a[i] = i;
i++;
}
i = 0;
do
{
a[i] = i;
i++;
} while (i < 5);