Nested Loops

Remember the general form of the for loop:

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

If we insert another for loop in place of the

  <statement>

above, we will have a nested for loop:

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

Example:

for (int i = 1; i <= 3; i++)
  for (int j = 1; j <= 3; j++)
    cout << j << endl;

Output:

1
2
3
1
2
3
1
2
3
Previous page
Next page

Back to Lesson 10 Index
Back to Outline