Execution of a for Loop
- The control variable is assigned an initial value
- The boolean expression (terminating condition) is evaluated
- If the terminating condition is TRUE then
a) the body of the loop is executed
b) the update expression is evaluated
- 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