Do While Loop

This section describes the third type of looping or repetition structure, called a do…while
loop. The general form of a do…while statement is as follows:

Of course, statement can be either a simple or compound statement. If it is a
compound statement, enclose it between braces. 

In C++, do is a reserved word.
The statement executes first, and then the expression is evaluated. If the expression
evaluates to true, the statement executes again. As long as the expression in a
do…while statement is true, the statement executes. To avoid an infinite loop, you
must, once again, make sure that the loop body contains a statement that ultimately makes
the expression false and assures that it exits properly.


i = 0;
cout << i << ” “;
i = i + 5;
while (i <= 20);

The output of this code is:

0 5 10 15 20


i = 11;
while (i <= 10)
cout << i << ” “;
i = i + 5;
cout << endl;