Execution of a for Loop

  1. The control variable is assigned an initial value
  2. The boolean expression (terminating condition) is evaluated
  3. If the terminating condition is TRUE then

  4. a) the body of the loop is executed
    b) the update expression is evaluated
  5. If the terminating condition is FALSE then control of the program is transferred to the first statement that follows the for loop

This example counts down by 2:

int a;
for (a = 10; a >= 2; a = a - 2)
  cout << a << endl;

Output:
10
8
6
4
2

Previous slide
Next slide

Back to Lesson 9 Index
Back to Outline