Notes on the while Loop
- The boolean condition is any valid statement that can be evaluated to TRUE or FALSE.
- The boolean expression must have a value before the while loop is encountered. This is called "priming the loop."
- At least one statement in the body of the while loop must change a variable contained within the conditional statement. Otherwise, the condition will remain the same and you'll end up with an "infinite" loop.
- At some point in the body of the loop, the conditional statement must evaluate to FALSE. Otherwise, it is an infinite loop.
Examples of infinite loops:
int a = 1;
while (a < 5)
cout << a << endl;
int b = 1;
while (b != 10)
{
cout << b << endl;
b = b + 5;
}
Previous slide
Next slide
Back to Lesson 9 Index
Back to Outline