Nested Loops in C++ - dummies

# Nested Loops in C++

The body of a loop can itself contain a loop; this arrangement is known as nested loops. The inner loop must execute to completion during each time through the outer loop.

A C++ program used nested loops to create a multiplication table in the following form:

```    0    1    2    3    4    5    6    7    8    9
0  0*0  0*1  0*2  0*3  0*4  0*5  0*6  0*7  0*8  0*9
1  1*0  1*1  1*2  1*3  1*4  1*5  1*6  1*7  1*8  1*9
2  2*0  2*1  2*2  2*3  2*4  2*5  2*6  2*7  2*8  2*9
//... and so on...```

You can see that for row 0, the program has to iterate from column 0 through column 9. The program repeats the process for row 1, again for row 2, and so on right down to row 9. This implies the need for two loops: an inner loop to iterate over the columns and a second outer loop to iterate over the rows.

Each position in the table is simply the row number times the column number.

This is exactly how the following NestedLoops program works:

```//
//  NestedLoops - this program uses a nested loop to
//                calculate the multiplication table.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
int nColumn = 0;
cout << "    ";
while (nColumn < 10)
{
// set the display width to two characters
// (even for one digit numbers)
cout.width(2);
// now display the column number
cout << nColumn << " ";
// increment to the next column
nColumn++;
}
cout << endl;
// now go loop through the rows
int nRow = 0;
while (nRow < 10)
{
cout << nRow << " - ";
// go through column 9
nColumn = 0;
while(nColumn < 10)
{
// display the product of the column*row
// (use 2 characters even when product is
// a single digit)
cout.width(2);
cout << nRow * nColumn << " ";
// go to next column
nColumn++;
}
// go to next row
nRow++;
cout << endl;
}
// wait until user is ready before terminating program
// to allow the user to see the program results
cout << "Press Enter to continue..." << endl;
cin.ignore(10, 'n');
cin.get();
return 0;
}```

The first section creates the column headings. This section initializes nColumn to 0. It then iterates through nColumn printing out its value separated by a space until nColumn reaches 10. At this point, the program exits the first loop and then tacks a new line on the end to finish the row. The figure illustrates this process graphically.

Executing just this section alone generates the following output:

`0  1  2  3  4  5  6  7  8  9`

The expression cout.width(2) sets the display width to two columns — C++ will pad a space on the left for single-digit numbers.

The second set of loops, the nested loops, starts at nRow equal to 0. The program prints out the row number followed by a dash before launching into a second loop that starts nColumn at 0 again and iterates it back up to 9. For each pass through this inner loop, the program sets the output width to two spaces and then displays nRow * nColumn followed by a space.

The display width resets itself each time you output something, so it’s necessary to set it back to 2 each time before outputting a number.

The program outputs a newline to move output to the next row each time it increments nRow. This is shown graphically here:

The output from this program appears as follows:

```     0  1  2  3  4  5  6  7  8  9
0 -  0  0  0  0  0  0  0  0  0  0
1 -  0  1  2  3  4  5  6  7  8  9
2 -  0  2  4  6  8 10 12 14 16 18
3 -  0  3  6  9 12 15 18 21 24 27
4 -  0  4  8 12 16 20 24 28 32 36
5 -  0  5 10 15 20 25 30 35 40 45
6 -  0  6 12 18 24 30 36 42 48 54
7 -  0  7 14 21 28 35 42 49 56 63
8 -  0  8 16 24 32 40 48 56 64 72
9 -  0  9 18 27 36 45 54 63 72 81
Press Enter to continue . . .```

There is nothing magic about 0 through 9 in this table. You could just have easily created a 12 x 12 multiplication table (or any other combination) by changing the comparison expression in the three while loops. However, for anything larger than 10 x 10, you’ll need to increase the minimum width to accommodate three-digit products: Use cout.width(3).