The for Loop

The general form of the for loop:

for (<initialization>; <boolean expression>; <update expression>)
  <statement>

  1. The initialization sets the control variable to some initial value. This variable will control the number of times the body of the loop is executed. This is executed only once at the beginning of the iterations.
  2. The boolean expression contains the control variable, and is evaluated before each iteration of the loop. As long as this expression is TRUE, the loop continues to iterate. This is also called the terminating condition.
  3. The update expression also contains the control variable. This is where the control variable is changed after each iteration of the loop body.

Example:

int a;
for (a = 1; a <= 5; a = a + 1)
  cout << a << endl;


Output:
1
2
3
4
5

Previous slide
Next slide

Back to Lesson 9 Index
Back to Outline