Everyone Can Learn Program
中文 Start learning
Course 1 · Zero to codingLesson 6 of 10Free to read

Loops inside loops · Robot Quest

What if the repeating part itself contains a repeat?

Course contents

Why you need it

Many tasks repeat at two levels: three rows, and in each row, four seats; four sides of a square, and on each side, several steps. A nested loop (a loop inside another loop) describes exactly this.

The intuition

A clock: the minute hand goes round 60 times for every one turn of the hour hand. The inner loop finishes completely before the outer loop moves on.

for side in range(4):       # outer: 4 sides
    for step in range(3):   # inner: 3 steps per side
        move()
    turn_right()            # once per side

move() runs 4 × 3 = 12 times. turn_right() belongs to the outer loop (it is indented once), so it runs 4 times. Indentation tells you which loop a line belongs to.

Definition

Nested loop

A nested loop is a loop placed inside the body of another loop; the inner loop runs completely for each round of the outer loop.

Examples

Three groups of three

Run it in the lesson

The outer loop counts groups; the inner loop handles the gems in one group.

for group in range(3):
    for gem in range(3):
        move()
        pick()
    move()
Step by step
  1. L1Outer round 1 (group = 0).group = 0
  2. L3The inner loop runs 3 full rounds: move, pick, three times.gem = 0, 1, 2
  3. L5Back in the outer body: one move over the empty square.group = 0
  4. L1Outer round 2 starts the inner loop again from gem = 0.group = 1
  5. •After 3 outer rounds: 9 gems picked and Bit is on the flag.

Around the pillar

Run it in the lesson

Three sides, two steps each, a right turn after every side.

for side in range(3):
    for step in range(2):
        move()
    turn_right()
Step by step
  1. L3Side 1: two moves east.
  2. L4Turn right once, at the end of the side.
  3. L3Side 2: two moves south.
  4. L3Side 3: two moves west, onto the flag.

Common mistakes

Wrong indentation level

A line indented 8 spaces is in the inner loop; 4 spaces is in the outer loop. Moving a line one level changes how often it runs.

Reusing the same counter name

Give the two loops different names (side, step). Using i for both is confusing and causes bugs later when you use the counters.

Quick quiz

1. How many times does move() run?

for a in range(2):
    for b in range(3):
        move()
  1. A5
  2. B6
  3. C3
Show the answer

Answer: B

The inner loop runs 3 times for each of the 2 outer rounds: 2 × 3 = 6.

2. Where does Bit end up?

for side in range(3):
    for step in range(2):
        move()
        turn_right()
  1. AOn the flag
  2. BIt crashes on line 3
  3. CTop-right corner
Show the answer

Answer: B

turn_right() is inside the inner loop, so Bit turns after every step and walks into the pillar.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictTwo sides

    Predict where Bit ends up.

  2. ModifyBigger groups

    The new map has 2 groups of 4 gems. Change the numbers so Bit collects them all and reaches the flag.

  3. WriteGem ring

    Collect all 9 gems around the block, then step onto the flag. Use a nested loop, at most 6 lines.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Nested loop嵌套循环A loop inside the body of another loop.
Inner loop内层循环The loop inside; it runs completely for each round of the outer loop.
Outer loop外层循环The loop that contains another loop.