The while Loop
The general form of the while loop:
while (<boolean statement>)
<statement>
Execution of the while statement:
- The boolean statement is evaluated to TRUE or FALSE.
- 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.
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