Skip to content

Nested loops

Trace nested for loops, count how many times the inner body runs, and write them to print a multiplication table and a triangle of stars.

It opens on

This program prints row * col for row from 1 to 3, and for col from 1 to 4 on each row. How many numbers will it print in all: 7, 12 or 81?

Why it matters

Anything laid out in rows and columns (a spreadsheet, an image, a seating plan, a game board) is processed by a loop over rows with a loop over columns inside it, and the cost of such a program is the product of the two counts.

Good to know first

  • for loops and range: range(1, 4) gives 1, 2, 3 and the loop variable takes each in turn
  • Loop patterns: a variable set before a loop and changed inside it
  • Input, output and type conversion: print writes its values and ends the line

Step by step

  1. A loop inside a loop

    Trace a nested loop pass by pass and count the inner body's runs as rows times columns.

    For row 1 the inner loop takes col 1, 2, 3 and 4, and prints 1 × 1, 1 × 2, 1 × 3 and 1 × 4, which are 1 2 3 4. Then print() ends the line.

    the first four lines of the trace

    A common mix-up

    The inner loop carries on where it stopped, so col is 5 on the second row.

    In fact: The inner for line runs again on every outer pass, so col starts again at 1.

  2. A triangle: the inner range depends on the outer

    Write and trace a nested loop whose inner range uses the outer variable, and count the inner runs as a sum.

    On row 2 the inner loop runs twice and prints two stars on one line, because end is the empty string "" and writes nothing after each star.

    the first two rows of the table

    A common mix-up

    The inner body runs outer count times inner count, so 4 × 4 = 16 here.

    In fact: When the inner range depends on the outer variable the counts differ on each row and are added, giving 10.

  3. Where print() goes

    Predict the output of nested loops whose print() is at different levels of indentation, and say how often a line runs from its indentation.

    Each row, the inner loop prints ### with three runs, and then print() ends the line, once per row. The output is the line ### twice, six hashes in all, from 2 × 3 = 6 inner runs.

    A common mix-up

    Print() ends the line wherever it is written in the block.

    In fact: It ends a line each time it runs, so its indentation decides how many line ends there are.

Try it

What does this print: for r in range(2), for c in range(1, 4), print(r + c, end=" "), then print() after the inner loop?Answer

1 2 3 on the first line and 2 3 4 on the second

Hint: r is 0, then 1

How many times does the inner body run for row in range(1, 6) with the inner loop for col in range(row)?Answer

15

Hint: 1 + 2 + 3 + 4 + 5

What does the version with print() inside the inner loop print for range(2) outer and range(2) inner?Answer

Four lines, each with a single #

Hint: The inner body runs 2 × 2 = 4 times

What does the inner loop print on the row where row is 2?Answer

2 4 6 8, since row * col with col from 1 to 4.

How many times does the inner body run on the row where row is 3, and what line does it print?Answer

3 times, and it prints ***.

Nested loops

30 minutes every month. No card.