Repeat Until at the Goal
So far, the dog knew the path in advance, and you could count every step. Real programs, however, have to work for data their author doesn't know in advance: a satnav for every route, a calculator for every number. That's why this exercise has three maps. You'll find them in the tabs above the dog. When you run the program, the dog tries it on the first map, then the second and finally the third, and you complete the exercise only if it reaches the goal on all of them.
The paths have different lengths, so Repeat 3 times won't help: a number that fits one map won't fit the others. You need a loop that doesn't repeat blocks a given number of times, but until something is true. That's the new Repeat until at the goal block:
- Before every pass, the dog checks whether it's at the goal already.
- If it is, the loop ends and the program goes on with the blocks under the loop.
- If it isn't, it carries out the blocks in the loop and goes back to step 1.
Such a loop doesn't have a fixed number of passes. On a short map, it runs three times; on a long one, ten times.
Watch out for one thing: if the blocks in the loop never get the dog to the goal (for example, if it only turns on the spot), the loop would never end. Such a loop is called an endless loop, and you'll learn more about it at the end of this lesson.
Instructions
Get the dog to the goal on all three maps.
Use at most 2 blocks.
Start programming for free
1/4