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

Pick up all three bones.

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

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

7/8