HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 6 Statements

Looping Statements

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Syntax

while (expression) statement
do statement while (expression);
for ([expression1] ; [expression2]; [expression3])
      statement

Description

You 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

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;
}

© Hewlett-Packard Development Company, L.P.