Please enable JavaScript to use CodeHS

Chapter 4

Iteration

4.4 Nested Iteration

Earlier in this unit, you saw how to use a loop for a repeated task. For example, if you wanted to print the numbers 1 through 5 on a line, you could use a loop like this:

for(int count = 1; count < 6; count++) 
{ 
   System.out.print(count + " "); 
}
Plain text

What if you wanted to print something like this?

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25 
Plain text

You could use 5 loops, one for each line and each loop starting and stopping at different values. This would get repetitive and you saw how you could use loops to avoid repetitive code. In this section, you are going to explore the concept of a nested loop.

Nested for Loops

Just as you saw with if statements, putting a loop inside another loop is called nesting. A nested iteration statement is an iteration statement that appears in the body of another iteration statement.

In the example above, you can achieve the five-by-five print out using a nested for loop.

// New loop outside
for (int row = 1; row < 6; row++) {
    // Original loop inside
    for(int count = 1; count < 6; count++) {
        System.out.print(count * row + " " );
    }
    System.out.println();
}
Plain text

The original loop is now wrapped inside of a second loop. Watch how the execution plays out.

Each time through the outer loop, the inside loop starts over and runs to completion. After completing the inside loop and printing a row, a line feed is added, then the outer loop loops around and starts the next row.

In this example, the inner loop executes 5 times each time it is called. The outer loop also executes 5 times, each time creating a new inner loop. So each line of code inside the inner loop actually executes 25 times!

Nested while Loops

Just as there are nested for loops, you can also have nested while loops, or even mixed nested loops with a for loop inside a while loop, or vice versa.

int line = 1;
while(line < 6) {
    int number = 1;
    while(number < 6) {
        System.out.print(number*line +" ");
        number++;
    }
    System.out.println()
    line++;
}
Plain text

Make a Rectangle

Nested Loop Iteration Counts

Inverted Triangle

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    How often is the inner loop of a nested loop run?

Exercise: Addition Table

In this program, you need to make an addition table. Your output should look like:

1   2   3   4   5   6   7   8   9   10  
2   4   5   6   7   8   9   10  11  12  
3   5   6   7   8   9   10  11  12  13  
4   6   7   8   9   10  11  12  13  14  
5   7   8   9   10  11  12  13  14  15  
6   8   9   10  11  12  13  14  15  16  
7   9   10  11  12  13  14  15  16  17  
8   10  11  12  13  14  15  16  17  18  
9   11  12  13  14  15  16  17  18  19  
10  12  13  14  15  16  17  18  19  20
Plain text

Modify the makeAdditionTable() method in the starter code to create this!

Hint: To evenly space these, you may want to use \t.