The do...while Loop

The do...while loop is a post-test loop. It is very similar to the while loop, but it is used less often.

The general form of the do...while loop:

do
   <statement>
while (<boolean statement>)

Execution of the while statement:

  1. The body of the loop is executed.
  2. The boolean statement is evaluated.
  3. If the statement evaluates to TRUE, the body of the loop is executed again.
  4. If the statement evaluates to FALSE, control is transferred to the statement following the loop.

Example:

int a = 8;
do
{
  cout << a << endl;
  a++;
}while (a < 5);

Output:
8
Previous slide
Next slide

Back to Lesson 9 Index
Back to Outline