Study CommonsRead · reason · practise

Home / Reasoning / Nested Loops: A Repeat Inside a Repeat

Reasoning & Coding · Coding

Nested Loops: A Repeat Inside a Repeat

Trace loops inside loops, multiply to count how many times the inside steps run, print rows of stars and walk a robot around a grid with a nested repeat.

Learning objectiveTrace a loop inside another loop, work out how many times the inside steps run by multiplying, and predict the output of a nested loop in number, pattern and robot-path programs.

The idea

An egg carton has 2 rows with 6 eggs in each row. To count the eggs you do not add 2 and 6. You count 6 eggs, then count 6 eggs again, once for each row. A program can count the same way with a loop inside another loop. This is called a nested loop.

In earlier lessons, Repeat 3 [say *] meant do the steps in the brackets 3 times: * * *. In a nested loop, the steps in the brackets include another Repeat. The outside loop is the outer loop and the loop inside it is the inner loop.

Here is the key rule. Every time the outer loop starts a new pass, the inner loop starts again from the beginning and runs all of its repeats. In Repeat 3 [Repeat 2 [say *]], the outer loop makes 3 passes and each pass runs the inner loop 2 times. So say * runs 3 × 2 = 6 times. Multiply the repeat numbers; do not add them.

Steps can sit in the outer loop outside the inner loop. In Repeat 2 [say A; Repeat 3 [say B]], each outer pass says A once, then B three times. The output is A B B B A B B B. Read the brackets carefully to see which loop each step belongs to.

Nested loops make rows. In this lesson, say * adds one star to the current row, and next line starts a new row. Putting next line in the outer loop, after the inner loop, ends each row after its stars are drawn. A loop can also count with a name: For r from 1 to 3 means r is 1 on the first pass, 2 on the second and 3 on the third.

Robot programs use the same rules as earlier lessons: F moves forward one square, L and R turn a quarter turn without moving, S is the start and dark squares are walls. The robot starts on S facing right (east). A nested repeat such as Repeat 4 [Repeat 2 [F] R] means: go forward 2, turn right, and do that four times.

Worked examples

Example 1 · Count how many times the inside runs

Program: Repeat 3 [Repeat 2 [say *]]. What is said, and how many stars is that?

  1. Outer pass 1: the inner loop runs its 2 repeats and says * *.
  2. Outer pass 2: the inner loop starts again and says * *. Outer pass 3 does the same.
  3. Total: 3 passes × 2 stars = 6 stars. It is not 3 + 2 = 5.

It says * * * * * * (6 stars).

Example 2 · Make rows with next line

Program: Repeat 2 [Repeat 4 [say *]; next line]. What does it draw?

  1. Outer pass 1: the inner loop adds 4 stars to the row. Then next line starts a new row.
  2. Outer pass 2: the inner loop adds 4 stars to the new row. Then next line starts another new row.
  3. next line is outside the inner loop, so it runs once per outer pass, not once per star.

2 rows of 4 stars: **** and ****

Example 3 · Walk around a square

Program: Repeat 4 [Repeat 2 [F] R]. The robot starts on S facing right. Where does it finish?

A 3 by 3 grid. S is the top left square. A wall fills the centre square. All other squares are open.S
A 3 by 3 grid. S is the top left square. A wall fills the centre square. All other squares are open.
  1. Outer pass 1: F F moves two squares right to the top right corner. R turns the robot to face down.
  2. Outer pass 2: F F moves down to the bottom right corner. R turns it to face left. Outer pass 3: F F moves left to the bottom left corner. R makes it face up.
  3. Outer pass 4: F F moves up to S. R turns it to face right again.

It finishes back on S, facing right, after walking all the way around the wall.

Common mistakes

Adding the repeat numbers instead of multiplying.

Why this fails: Each outer pass runs the whole inner loop. Repeat 3 [Repeat 4 [...]] runs the inside steps 3 × 4 = 12 times, not 3 + 4 = 7.

Thinking the inner loop only runs on the first pass.

Why this fails: The inner loop starts again from the beginning on every outer pass.

Putting next line inside the inner loop.

Why this fails: Then every star gets its own row. To end a row after its stars, next line belongs in the outer loop, after the inner loop.

Mixing up which steps belong to which loop.

Why this fails: Look at the square brackets. A step inside only the outer brackets runs once per outer pass. A step inside the inner brackets runs once per inner repeat.

Setting a counter back to 0 inside the outer loop.

Why this fails: Each outer pass would erase the count so far. Set a total to 0 once, before both loops.

Foundation

Foundation practice

Count how many times the inside runs and trace short nested loops.

Concept

1. A program has a loop inside another loop. What does the inner loop do each time the outer loop starts a new pass?

  1. It runs only on the first pass
  2. It runs once in total, after the outer loop ends
  3. It starts again and runs all of its repeats
  4. It carries on from where it stopped last time
Count the passes

2. Program: Repeat 3 [Repeat 4 [say x]]. How many times is x said?

  1. 12 times
  2. 7 times
  3. 3 times
  4. 4 times
Trace

3. Program: Repeat 2 [say A; Repeat 3 [say B]]. What is said?

  1. A A B B B B B B
  2. A B B B
  3. A B A B A B
  4. A B B B A B B B
Trace

4. Program: Repeat 2 [Repeat 2 [say 1]; say 2]. What is said?

  1. 1 1 2
  2. 1 1 2 1 1 2
  3. 1 2 1 2
  4. 1 1 1 1 2 2
Robot moves

5. The robot runs Repeat 5 [Repeat 4 [F]] on a very large open grid. How many squares does it move forward?

  1. 9 squares
  2. 4 squares
  3. 20 squares
  4. 5 squares

Practice

Practice practice

Make rows of stars and walk the robot with nested repeats.

Pattern

6. Program: Repeat 3 [Repeat 5 [say *]; next line]. What does it draw?

  1. 5 rows of 3 stars
  2. 3 rows of 5 stars
  3. One row of 8 stars
  4. One row of 15 stars
Pick the program

7. Which program draws 2 rows of 4 stars?

  1. Repeat 4 [Repeat 2 [say *]; next line]
  2. Repeat 2 [Repeat 4 [say *]]
  3. Repeat 2 [Repeat 4 [say *; next line]]
  4. Repeat 2 [Repeat 4 [say *]; next line]
Totals

8. Program: total = 0; Repeat 4 [Repeat 3 [total = total + 2]]; say total. What is said?

  1. 24
  2. 14
  3. 12
  4. 9
Robot path

9. The robot starts on S facing right and runs Repeat 2 [Repeat 2 [F] R]. Where does it finish?

  1. One square above G, facing down
  2. Back on S, facing right
  3. On G, facing left
  4. On G, facing down
A 3 by 3 grid with no walls. S is the top left square and G is the bottom right square.SG
A 3 by 3 grid with no walls. S is the top left square and G is the bottom right square.
Robot path

10. The robot starts on S facing right. Which program walks it around the outside of the wall block and back to S?

  1. Repeat 3 [Repeat 4 [F] R]
  2. Repeat 4 [Repeat 3 [F] R]
  3. Repeat 4 [Repeat 4 [F] R]
  4. Repeat 3 [Repeat 3 [F] R]
A 4 by 4 grid. S is the top left square. The four centre squares are walls. All the squares around the outside edge are open.S
A 4 by 4 grid. S is the top left square. The four centre squares are walls. All the squares around the outside edge are open.

Challenge

Challenge practice

Growing patterns, loop counters and finding a bug.

Growing pattern

11. Program: For r from 1 to 3 [Repeat r [say *]; next line]. Rows are shown here separated by slashes. What does it draw?

  1. *** / *** / ***
  2. *** / ** / *
  3. * / * / *
  4. * / ** / ***
Count the stars

12. Program: For r from 1 to 4 [Repeat r [say *]; next line]. How many stars are drawn altogether?

  1. 16
  2. 10
  3. 14
  4. 4
Trace

13. Program: For a from 1 to 2 [For b from 1 to 3 [say a × b]]. What is said?

  1. 1 2 2 4 3 6
  2. 1 2 3 4 5 6
  3. 1 2 3 2 4 6
  4. 1 2 3 1 2 3
Debug

14. This program should say 6, but it says 2: total = 0; Repeat 3 [total = 0; Repeat 2 [total = total + 1]]; say total. What is the fix?

  1. Move total = 0 to before the outer Repeat
  2. Change Repeat 2 to Repeat 3
  3. Change total + 1 to total + 3
  4. Move say total inside the inner loop
Three loops

15. Program: Repeat 3 [Repeat 2 [Repeat 2 [say z]]]. How many times is z said?

  1. 7 times
  2. 10 times
  3. 6 times
  4. 12 times

Answer key

  1. 1. It starts again and runs all of its repeats
    Each outer pass restarts the inner loop from the beginning. Running only once or only on the first pass forgets the restart, and carrying on from where it stopped is how a single long loop works, not a nested one.
  2. 2. 12 times
    The outer loop makes 3 passes and each runs the inner loop 4 times: 3 × 4 = 12. Choosing 7 adds the repeat numbers, and choosing 3 or 4 ignores one of the loops.
  3. 3. A B B B A B B B
    Each outer pass says A once and then B three times, and there are two passes. A A B B B B B B puts both A steps first, A B B B ignores the outer loop, and A B A B A B treats the inner loop as 1 repeat.
  4. 4. 1 1 2 1 1 2
    In each outer pass the inner loop says 1 1 and then say 2 runs once. Two passes give 1 1 2 1 1 2. 1 1 2 is only one pass. 1 2 1 2 treats the inner loop as one repeat. 1 1 1 1 2 2 puts say 2 after both loops.
  5. 5. 20 squares
    The F step runs 5 × 4 = 20 times, and each F moves one square. 9 comes from adding 5 + 4. Choosing 4 or 5 uses only one of the loops.
  6. 6. 3 rows of 5 stars
    The inner loop draws 5 stars and next line ends the row, once per outer pass, so there are 3 rows of 5. 5 rows of 3 swaps the loops. One row of 8 adds the numbers, and one row of 15 leaves out the next line step, so the stars never break into rows.
  7. 7. Repeat 2 [Repeat 4 [say *]; next line]
    The inner loop makes the 4 stars and next line in the outer loop ends each row. The first wrong option makes 4 rows of 2 stars. The second has no next line, so it makes one row of 8 stars. The third has next line inside the inner loop, so every star lands in its own row.
  8. 8. 24
    The step total = total + 2 runs 4 × 3 = 12 times, so the total is 12 × 2 = 24. 14 adds the repeats first, (4 + 3) × 2. 12 forgets to add 2 each time, and 9 adds 4, 3 and 2.
  9. 9. On G, facing left
    Pass 1: F F to the top right corner, then R to face down. Pass 2: F F down to G, then R to face left. One square above G is where the robot is after only one F of the second pass. Back on S would need four passes around a ring. On G, facing down forgets the last R, which still runs at the end of pass 2.
  10. 10. Repeat 4 [Repeat 3 [F] R]
    Each side of the ring is 3 moves long and a ring has 4 sides, so the program is Repeat 4 [Repeat 3 [F] R]. Four forward moves in a row would leave the grid. Repeat 3 [Repeat 3 [F] R] stops after three sides, on the bottom left square, one side short of S.
  11. 11. * / ** / ***
    On pass r the inner loop repeats r times. Pass 1 draws 1 star, pass 2 draws 2 and pass 3 draws 3. The option with 3 stars in every row uses a fixed inner count. The option that shrinks reverses the order, because r counts up.
  12. 12. 10
    The rows have 1, 2, 3 and 4 stars: 1 + 2 + 3 + 4 = 10. 16 would be right only if every row had 4 stars. 14 is a slip when adding the row counts, and 4 is just the last row.
  13. 13. 1 2 3 2 4 6
    While a is 1, b goes 1, 2, 3 and says 1 2 3. Then a becomes 2 and b starts again: 2 4 6. Putting 1 2 2 4 3 6 first holds b still and changes a, which swaps the loops. 1 2 3 4 5 6 keeps counting instead of multiplying, and 1 2 3 1 2 3 ignores a.
  14. 14. Move total = 0 to before the outer Repeat
    The step total = 0 inside the outer loop wipes the count at the start of every pass, so only the last pass is left: 2. Setting total to 0 once, before both loops, keeps all 3 × 2 = 6 counts. The other fixes keep the reset: Repeat 3 inside would still say 3, total + 3 would say 3, and say total inside the inner loop would say 1 2 1 2 1 2.
  15. 15. 12 times
    Three nested loops multiply all three numbers: 3 × 2 × 2 = 12. 7 adds them. 10 is 3 × 2 + 2 + 2, which multiplies only the first two loops. 6 uses only two of the loops.

Related practice