Ages 8+ · Coding · Loops
Loops inside loops
1 repeat 3 times 2 repeat 4 times 3 lay a stone 4 step down Three rows of four stones: a loop inside a loop. The big idea A 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 times How 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 The outside loop says repeat 3 times. The inside loop says repeat 4 times: lay a stone. The inside loop lays one whole row of 4 stones each time. Three rows of four make 3 x 4 = 12 stones. 1 repeat 3 times 2 repeat 4 times 3 lay a stone 4 step down Line 3 runs 3 x 4 = 12 times. Example 2: Step down Step down comes after the inside loop, but inside the outside loop. It runs once for each round of the outside loop. With 3 rounds, Bo steps down 3 times, once for every row. Example 3: The bug Step down is moved inside the inside loop. Now Bo steps down after every stone, so the stones march off the bed into the carrots. The mix-up to fix: move step down out of the inside loop, to just after it. 1 repeat 3 times 2 repeat 4 times 3 lay a stone 4 step down Line 4 is inside the inside loop: it runs 12 times. Example 4: Setting a table The outside loop goes round once for each person, and the inside loop puts out a fork, a spoon and a cup. For 3 people that is 3 x 3 = 9 things. 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 Loops multiply. Three rows of four is 3 x 4 = 12, not 3 + 4. Where a step sits matters. Inside the inside loop it runs 12 times. After it, only 3 times. Count the inside loop once for each round of the outside loop. Check which loop each number belongs to before you multiply. Try it The outside loop is 2 and the inside loop is 5. How many times does the inside step run? The outside loop is 4 and the inside loop is 3. How many stones are laid? A step sits after the inside loop. The outside loop is 5. How many times does it run? Do nested loops add or multiply? 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 home Set 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-ups Do 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.