The while Loop

The general form of the while loop:

while (<boolean statement>)
  <statement>

Execution of the while statement:

  1. The boolean statement is evaluated to TRUE or FALSE.
  2. If the statement evaluates to TRUE, then the body of the while loop is executed. After the last statement in the body has been executed, control returns to the top, and the boolean statement is evaluated again.
  3. If the statement evaluates to FALSE, control is transferred to the statement following the while loop.

Example:

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

Output:
1
2
3
4

Previous slide
Next slide

Back to Lesson 9 Index
Back to Outline