Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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 navigation app 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:

  1. Before every pass, the dog checks whether it's at the goal already.
  2. If it is, the loop ends and the program goes on with the blocks under the loop.
  3. 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, eleven 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

  1. 1

    Get the dog to the goal on all three maps.

  2. 2

    Use at most 2 blocks.

Back

1/8

  1. 1. Repeat Until at the Goal
  2. 2. Stairs of Any Height
  3. 3. While There's a Path Ahead
  4. 4. Corner After Corner
  5. 5. Quiz: Only What's Ahead
  6. 6. Bones All the Way
  7. 7. An Endless Loop
  8. 8. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.