Loops inside loops
After this: Put a loop inside a loop and say how many times the inner lines run.
On this page · 5 sections · 3 checks
A hall has three rows of seats with four seats in each row. To check every seat, you walk along row 1 seat by seat, then along row 2, then along row 3. That is twelve seats, and you never had to count to twelve: you counted to 3 for the rows and to 4 for the seats in each row.
A loop inside a loop
The lines of a loop can hold anything, including another loop. The outer loop picks a row. For that row, the inner loop goes through every seat. When the inner loop has nothing left, the outer loop moves on to the next row, and the inner loop starts again from the first seat. Two loops written this way are called .
The smallest useful example, in the trace below, is two rows of three, with a box that counts how many times the inner line runs.
rowis 1. The inner loop runs withcol1, 2 and 3, andcountbecomes 1, 2, 3.- The inner loop has nothing left. The outer loop goes to its next value:
rowis 2. - The inner loop starts again:
col1, 2, 3, andcountbecomes 4, 5, 6. - Both loops are finished. 6 is printed.
- Run the programRun it. Watch col run through 1, 2, 3 while row stays at 1, and only then watch row change.
count = 0for row in range(1, 3): for col in range(1, 4): count = count + 1print(count)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Watch the jumps. "The loop goes back to line 35" is the inner loop taking its next value; "The loop goes back to line 24" is the outer loop taking its next value. The inner box col runs through all of its values before row changes once. The inner box changes fastest.
The rule: the inner lines run once for every pair of values, so the number of passes is the outer count times the inner count. Two rows of three is 2 × 3 = 6. Three rows of four is 3 × 4 = 12.
A times table
Each pass of the inner loop can print one line. Here is a small times table: the outer loop picks the row, the inner loop picks the column, and the line prints the product.
for row in range(1, 3): for col in range(1, 4): print(row, "x", col, "=", row * col)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Two rows and three columns give six printed lines, one per pass of the inner line.
Run it: three by four
Count the lines of output: twelve, in three groups of four. Change both end numbers to 11 and run again for the whole table up to ten times ten.
Check yourself
An outer loop makes three passes. Inside it, an inner loop makes four passes. How many times do the lines inside the inner loop run?
What does this print?
for a in range(1, 3): for b in range(1, 3): print(a, b)for (int a = 1; a < 3; a++) { for (int b = 1; b < 3; b++) { System.out.println(a + " " + b); }}In this program, right after the inner line has run for the fifth time, what do row and col hold?
count = 0for row in range(1, 3): for col in range(1, 4): count = count + 1print(count)int count = 0;for (int row = 1; row < 3; row++) { for (int col = 1; col < 4; col++) { count = count + 1; }}System.out.println(count);Honesty notes
- Put a loop inside a loop: the inner loop runs through all its values for each value of the outer loop.
- Multiply to count the passes: three by four runs the inner lines twelve times.
- Say which counter changes fastest: the inner one.
- Print a times table with two loops, one for the rows and one for the columns.