Introduction
15 minLast lesson ended with a promise and a complaint.
You built a square with one repeat block, and then I asked you to make the sides longer. It was not one number. You had to drag four more blocks into the loop's mouth, and it felt like going backwards.
Today that gets fixed, and the fix is the single most useful idea in the whole section: a loop can go inside another loop.
A loop in a loop's mouth
The repeat block's mouth takes any blocks at all — including another repeat block.
When that happens, the inner loop runs all the way through, every single time the outer loop goes round once. Five inside four is not nine. It is five, four times over: twenty.
The two numbers mean different things
This is the part to get straight before you touch anything:
| Loop | What its number means |
|---|---|
| inner | how long — how many blocks in one row |
| outer | how many — how many rows there are |
Swap them by accident and you get a wall 3 wide and 10 tall instead of 10 wide and 3 tall. It will look wrong immediately, and now you know why.
What sits between the loops
Here is the bit that catches everybody. The outer loop's mouth holds two things:
- the inner loop, which builds one row, and
- the blocks that get the Agent ready for the next row.
After laying a row the Agent is standing at the far end of it. Something has to lift it up a level and bring it back to the start, or the second row begins where the first one ended. That reset lives in the outer mouth, after the inner loop.
Say it out loud
Five times over: {lay a block and step forward}. Then go up one and come back. And do all of that five times.
A nested loop you can say in a sentence is a nested loop you understand.
Warm-up — counting the class photo
Look at a photo of a class standing in rows. Nobody counts the children one at a time. They say: five in a row, four rows — twenty.
You have been running loops inside loops since you learned to multiply.
One warning before you start
The Agent carries its blocks in slots, and one slot holds 64 blocks. Every build in this lesson is designed to fit inside that.
A wall 5 by 5 is 25 blocks. A wall 10 by 3 is 30. Go much past 64 and the Agent simply stops mid-build with nothing to say for itself — so from today, count your build before you run it. Rows times columns. That multiplication is the same one the loops are doing.
Building bigger than a slotful is exactly what the fill command is for, and that is Lesson 7.
What you will be able to do
- Put one repeat inside another and know which number does what.
- Work out what has to happen between one row and the next.
- Build a wall, and a hollow room, from a handful of blocks.
- Count a build before you run it, so you never run out halfway up.
Basic
20 minFollow along on your own computer. Everyone should finish this part with the same working program.
Step 1 — Keep the promise: the square, properly
Open your square program from Lesson 3. One side is still written out longhand, four place-and-move pairs in a row.
Put a repeat around just that side, and take out the duplicates:
Run it. The same square as last lesson.
Now change the inner 4 to a 6, and run it again. Then 9.
That is the promise kept. The side length is one number at last, and the outer loop — the four sides — never had to change. Last lesson this took four new blocks each time.
Step 2 — Which number is which
Try it the other way round, on purpose. Set the inner number to 3 and the outer to 9.
You get a shape with nine sides of three, which spirals round on itself and closes nothing. Inner is how long. Outer is how many. Put them back to 4 and 6.
Step 3 — Now build upwards: the wall
A wall is the same idea standing up. The inner loop lays a row. The outer loop stacks the rows.
Challenge 1
15 minA wall to order.
The wall in Basic is 5 by 5. Make one to a different size.
- Press
Cand open yourwallprogram. - Change it to build a wall 10 blocks wide and 3 blocks tall.
- Before you run it, work out how many stone blocks that needs and check it fits in one slot of 64. Write the number down.
- Go back to the world, type
wall, and count the wall to check it. - Now make it 6 wide and 6 tall instead, and run it again.
Watch out: making a wall wider is not a one-number change. Step 5 of Basic says why.
It works if: the first wall is exactly 10 blocks along and 3 high with no gaps and no steps, and the second is a neat 6 by 6 square of stone standing upright.
Challenge 2
20 minA room with four walls.
A wall is one side of something. Put four of them together and you have a room — and that needs a third loop.
- Press
Cand start a new chat command calledroom. - Bring the Agent to you and fill slot 1 with stone. Both stay outside every loop.
- Build a hollow room with a 5 by 5 footprint, 3 blocks tall, with no roof and no floor — just the four walls.
- You already have both halves of this. Lesson 3's square goes round four sides; today's wall stacks levels. You need one inside the other.
- Count it first: four sides of five, three levels high. Check it fits in 64.
- Type
room, then fly up and look down into it.
Think about which loop should be outermost. There is an order that needs no reset blocks at all, because a square always finishes where it started.
It works if: a hollow stone box 5 by 5 and 3 tall is standing there, all four walls closed, no roof, no floor — and you can drop into it from above.
Challenge 3
25 minA building somebody could use.
An open-topped box is not a building. Give it a way in.
- Fly to a clear, flat piece of ground and land.
- Press
Cand start a new chat command calledhut. - Build a hut with nested loops. It is yours to design, but it must have four closed walls at least 3 blocks tall, and a doorway you can walk through — two blocks tall, at ground level.
- The doorway must be made by the program, not by you afterwards. You met the block that removes blocks in Lesson 2.
- Budget: 64 blocks. Work out your total before you run it — sides times length times height. If it does not fit, make it smaller.
- Type
hut, then walk in through your own front door.
It works if: you can walk in at ground level without flying, jumping or breaking anything by hand, and the walls are closed everywhere else.
Extra challenge
spare timeThe floor.
Every build so far has been walls — hollow outlines with nothing underneath. Fill something in.
- Press
Cand start a new chat command calledfloor. - Build a solid square platform, 7 blocks by 7, flat on the ground with no holes anywhere in it.
- That is 49 blocks, so it just fits in one slot. Check the arithmetic before you run it.
- Type
floor, then walk right across it, corner to corner.
The nesting is the same shape as the wall, but the reset is different. A wall goes up a level between rows. A floor goes sideways.
It works if: you can walk over every part of the platform without falling through a gap, and it is square — the same number of blocks along each edge.
The ceiling you have just hit
A 7 by 7 floor is 49 blocks. An 8 by 8 is 64 — the whole slot, exactly. A 10 by 10 would be 100, and the Agent would stop three-quarters of the way through with an empty bag.
So there is a size of build that loops simply cannot reach, however cleverly you nest them. That is not a failure of loops; it is the Agent carrying things one slot at a time.
Lesson 7 introduces fill, which builds a whole region at once without the Agent walking anywhere or carrying anything. A 10 by 10 floor becomes a single block of program. Until then, 64 is your ceiling — and counting before you run is the habit that saves you.