previous | start | next

## Nested Loops

- Sometimes the iteration count of the inner loop depends on the outer loop.
- Example: printing a triangle shape.

  >     []
  >     [][]
  >     [][][]
  >     [][][][]

- To print `n` rows, use the loop.

  >     for (int i = 1; i <= n; i++)
  >     {
  >        print triangle row
  >     }

- Each row contains `i` boxes.

  >     for (int j = 1; j <= i; j++)
  >        cout << "[]";
  >     cout << "\n";

- Putting the two loops together yields

  >     for (int i = 1; i <= n; i++)
  >     {
  >        for (int j = 1; j <= i; j++)
  >           cout << "[]";
  >        cout << "\n";
  >     }

previous | start | next