Summary
In this lesson, you learned that:
- Something that repeats in a task is a pattern. Wherever there's a pattern, a loop can make the program shorter.
- A loop such as
Repeat 3 timescarries out the blocks in its mouth as many times as its number says. Carrying them out once is a pass through the loop. - On every pass, all the blocks inside are carried out from top to bottom, and only then does the next pass start.
- Blocks under a loop are carried out once, after all its passes.
- Inside a loop, the order of the blocks matters. In A Bone on Every Tile,
Move forwardhad to come beforePick up bone, so that the dog first stepped onto the bone. Go through the first and the last pass in your head. - A loop inside another loop is a nested loop. The inner loop runs all the way through on every pass of the outer one.
- Two kinds of bugs are common in loops: the wrong number of passes, and a block in the wrong place, inside the loop instead of under it or the other way around.
Time to practice. The stairs are back, but each stair is now three tiles wide, and there's a bone on each of those tiles. The last bone lies just above the top stair.
In the next lesson, Checkpoint: A Walk in the Park, you'll build whole programs on your own with what you know now.
Instructions
- 1
Pick up all ten bones. First work out what the dog does on one stair, and then where the last bone is.
- 2
Use at most 9 blocks. You'll need a nested loop.