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

A Loop in a Loop

The flower bed is bigger now, and each of its sides is six steps long. The program from the last exercise would need six Move forward blocks in a row. But six identical blocks in a row are a pattern again, one that a loop can replace.

You can put any blocks in a loop, including another loop. This is called a nested loop:

  • Repeat 3 times
    • Repeat 6 times
      • Move forward
    • Pick up bone
    • Turn right ↻

The outer loop has three passes, one for each side of the bed. On each of them, the whole inner loop runs, that is, six steps, followed by picking up the bone and turning. Altogether, the dog takes 3 × 6 = 18 steps, even though the program has a single Move forward block.

Nested loops are common in programming: for example, when a program goes through the rows of a table and, in each row, through all its cells.

Instructions

  1. 1

    Pick up all three bones.

  2. 2

    Use at most 5 blocks. You'll need a nested loop.

Back

8/11

  1. 1. Repeating Moves
  2. 2. Loops
  3. 3. Stairs
  4. 4. After the Loop
  5. 5. Quiz: Pass by Pass
  6. 6. A Bone on Every Tile
  7. 7. Around the Flower Bed
  8. 8. A Loop in a Loop
  9. 9. A Bug in a Loop
  10. 10. Quiz: A Bug in a Nested Loop
  11. 11. Summary
Next

Try it yourself

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