HPlogo HP C/HP-UX Reference Manual: Workstations and Servers > Chapter 6 Statements

Iteration Statements

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

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 loop exit tests.

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):

i = 0;
while (i < 5)
{
a[i] = i;
i++;
}
i = 0;
do
{
a[i] = i;
i++;
} while (i < 5);
for (i = 0; i < 5; i++)
{
a[i] = i;
}