Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 8+ · Coding · Loops
Loops inside loops
1repeat 3 times2 repeat 4 times3 lay a stone4 step down
Three rows of four stones: a loop inside a loop.
The big ideaA loop can sit inside another loop. The inside loop runs fully each round.

How it works

A loop can sit inside another loop, like a smaller doll inside a bigger doll.

The inside loop runs all the way through every time the outside loop goes round.

To count the total, multiply: the outside number times the inside number.

A step after the inside loop runs once for each round of the outside loop.

Outside 3, inside 4• before both loops: 1 time• inside the inside loop: 12 times• after the inside loop: 3 timesHow to work it out• once• 3 x 4 = 12• once for each round of 3
How often each step runs.

Example 1: Mina's path

  1. The outside loop says repeat 3 times. The inside loop says repeat 4 times: lay a stone.
  2. The inside loop lays one whole row of 4 stones each time.
  3. Three rows of four make 3 x 4 = 12 stones.
1repeat 3 times2 repeat 4 times3 lay a stone4 step down
Line 3 runs 3 x 4 = 12 times.

Example 2: Step down

  1. Step down comes after the inside loop, but inside the outside loop.
  2. It runs once for each round of the outside loop.
  3. With 3 rounds, Bo steps down 3 times, once for every row.

Example 3: The bug

  1. Step down is moved inside the inside loop.
  2. Now Bo steps down after every stone, so the stones march off the bed into the carrots.
  3. The mix-up to fix: move step down out of the inside loop, to just after it.
1repeat 3 times2 repeat 4 times3 lay a stone4 step down
Line 4 is inside the inside loop: it runs 12 times.

Example 4: Setting a table

  1. The outside loop goes round once for each person, and the inside loop puts out a fork, a spoon and a cup.
  2. For 3 people that is 3 x 3 = 9 things.
  3. For 4 people it is 4 x 3 = 12 things.

Words to know

nested loop
A loop inside another loop.
outside loop
The bigger loop that goes round and round.
inside loop
The smaller loop that runs all the way through each round.

Watch out

Try it

  1. The outside loop is 2 and the inside loop is 5. How many times does the inside step run?
  2. The outside loop is 4 and the inside loop is 3. How many stones are laid?
  3. A step sits after the inside loop. The outside loop is 5. How many times does it run?
  4. Do nested loops add or multiply?
  5. The outside loop is 3 and the inside loop is 5. How many stones?
Answers: 1. 10 · 2. 12 · 3. 5 · 4. Multiply · 5. 15
Try at homeSet the table with a loop inside a loop. The outside loop goes round once for each person at the table, and the inside loop puts down a fork, a spoon and a cup. How many things do you put out for three people? Check with a grown-up: it is nine. Then try four people.
For grown-upsDo the Mission: set the table together like a nested loop. The outside loop goes round once for each person, and the inside loop puts down a fork, a spoon and a cup. For three people it is nine things, and for four people twelve.
Open these notes on screenScan the code, or go to tutorpip.com.