HP C A.06.05 Reference Manual

Statements
do…while
Chapter 6 159
do…while
Syntax
do
statement;
while (
expression
);
Arguments
statement
A null statement, simple statement, or compound statement.
expression
Any expression.
Description
The do statement executes statements within a loop until a specified condition is satisfied.
This is one of the three looping constructions in C. Unlike the for and while loops, do…while
performs
statement
first and then tests
expression
. If
expression
evaluates to nonzero
(true),
statement
executes again, but when
expression
evaluates to zero (false), execution of
the loop stops. This type of loop is always executed at least once.
You can jump out of a do…while loop prematurely (that is, before
expression
becomes false)
by doing the following:
Use break to transfer control to the first statement following the do…while loop.
Use goto to transfer control to some labeled statement outside of the loop.
Use a return statement.
Example
/* Program name is "do.while_example". This program finds the
* summation (that is, n*(n+1)/2) of an integer that a user
* supplies and the summation of the squares of that integer.
* The use of the do/while means that the code inside the loop
* is always executed at least once.
*/
#include <stdio.h>
int main(void)