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

Which Way at the Corner?

On each of these maps, the path goes straight, turns once and goes on straight to the goal. But on some maps, it turns left, and on others, right. A program with Turn left ↺ at the corner would work on only some of them. At the corner, the dog has to look around and decide which way to go.

Two new senses help it: path to the left and path to the right. They look at the tile beside the dog, on its left or on its right, and answer true or false, just like path ahead. Left and right are from the dog's point of view, just as with turning. On the third map, the dog faces down the screen, toward you, so its left is on your right.

At the corner, the path goes on either to the left or to the right. That's a choice between two options, so it's a job for If … else:

  • If path to the left
    • Turn left ↺
  • else
    • Turn right ↻

One question is enough: if the path doesn't go on to the left, it must go on to the right. You could just as well ask about path to the right and swap the turns.

This If isn't inside a loop. The dog needs to decide only once, at the corner, so the If goes between the two straight parts of the walk:

  1. Walk to the corner: Repeat while path ahead with Move forward, as in the While There's a Path Ahead exercise.
  2. Decide which way to turn.
  3. Walk on to the goal.

The program carries out its blocks from top to bottom, and an If is no exception. When the program gets to it, the dog decides, carries out one of the two parts and goes on with the blocks under the If.

Instructions

  1. 1

    Get the dog to the goal on all three maps: walk to the corner, turn the right way and walk on to the goal.

Back

3/9

  1. 1. The If Block
  2. 2. If, Else
  3. 3. Which Way at the Corner?
  4. 4. Quiz: One Part per Pass
  5. 5. An If Inside an If
  6. 6. A Path with Bones
  7. 7. Quiz: A Bug in a Nested If
  8. 8. Side Trips
  9. 9. Summary
Next

Try it yourself

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