Loops

When something in a program repeats, you don't have to build it again and again. You just tell the computer what to repeat and how many times. That's what a loop is for, here the Repeat 3 times block.

The loop block is shaped like the letter C. The blocks you put in its "mouth" are carried out as many times as the number in the block says. To change the number, click it and type another one. The program

  • Repeat 3 times
    • Move forward

does the same as three Move forward blocks one under another. Carrying out the blocks inside a loop once is called a pass through the loop (or an iteration). This loop has three passes.

Why is a loop better than blocks one under another?

  • The program is shorter, and you can see at a glance what it does: "move forward three times".
  • It's easy to change. When the path gets longer, you change one number instead of adding blocks.
  • Fewer mistakes. In a long row of identical blocks, it's easy to add or leave out one.

Every programming language has loops, and you'll use them all the time.

Instructions

Get the dog to the goal. Count how many steps lead to it.

Use at most 2 blocks: one Repeat with Move forward inside it.

Start programming for free

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

Or sign up with:

Back

2/8

Next