Notes on the while Loop

  1. The boolean condition is any valid statement that can be evaluated to TRUE or FALSE.
  2. The boolean expression must have a value before the while loop is encountered. This is called "priming the loop."
  3. 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.
  4. 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