Skip to main content

Section 8.8 Nested Loops

Subsection 8.8.1 Basics

Like conditional statements, loops can be nested one inside the other. Nesting loops allow you to iterate over all the possible values for one variable for each value of the other variable. Among other things, this is useful for generating 2-dimensional tables. For example, a โ€œmultiplication table up to 8x8โ€ needs to take each number from 1-8 and then multiply it by each value from 1-8:
Listing 8.8.1.
In this example, the first loop (for i) is known as the โ€œouter loopโ€, and the second loop (for j) is known as the โ€œinner loopโ€. The outer loop is responsible for iterating over the rows. For each row, the inner loop repeats for each of the 8 columns, printing out the row number i multiplied by the column number j followed by a tab. After the inner loop finishes, a newline is printed to start a new row. Then the outer loop continues to the next row. This process can be seen in the Codelense example below. It has the same structure, but produces a smaller table and calls the counters row and col` for clarity:
Listing 8.8.2.
The two loops create multiple areas to write code. Anything before the inner loop happens once per iteration of the outer loop at the start of the โ€œrowโ€. Anything in the inner loop happens at each โ€œcellโ€ (row/col). Anything after the inner loop happens once at the end of the โ€œrowโ€:
Listing 8.8.3.
for (int i = 0; i < 8; i++) {
    // Anything that is done at the start of each "row"
    for (int j = 0; j < 8; j++) {
        // Anything that is done for each "cell"
    }
    // Anything that is done at the end of each "row"
}
This is especially important to keep in mind when nesting while loops. We need the inner loopโ€™s counter to reset EVERY time the outer loop iterates. That means the inner loop counter needs to be declared inside the outer loop. Also, we need to make sure that our updates to the loop control variables happen at the end of the appropriate loop body:
Listing 8.8.4.
int i = 0;
while (i < 8) {
    int j = 0;  // inner loop counter starts at 0 every time
    while (j < 8) {
        // do work 
        ++j;  // next step of inner loop
    }
    ++i;  // next step of outer loop
}

Subsection 8.8.2 Other uses of Nested Loops

Console output always needs to happens line by line, hence our focus on the outer loop being responsible for the โ€œrowโ€s. If we were processing data in a spreadsheet, we could have the outer loop be responsible for columns and the inner loop handle each row in that column. We will see examples like that later on.
Finally, it is worth noting that there are many times we will need to use nested loops that donโ€™t involve printing a table. Some possible examples:
  • For each line in a file, read in 10 numbers. (Outer loop is lines, inner is numbers on the line)
  • For each student, loop through their classes to add up their credits. (Outer loop is students, inner is classes that student is taking)
  • For each number in a list, find its distance from every other number in the list to determine the closest two numbers. (Outer loop is numbers, inner loop is the same list of numbers.)

Checkpoint 8.8.1.

Put the blocks in order so that they make a 5 row by 10 column grid where each row has the row number, a :, and then the column numbers 1-10. Like this:
1:    1   2   3   4   5   6   7   8   9   10
2:    1   2   3   4   5   6   7   8   9   10
3:    1   2   3   4   5   6   7   8   9   10
...
You have attempted of activities on this page.