Chapter 4

Loops and Repetition

Use repeat, forever, and repeat until loops to make blocks run again and again.

Sometimes you want a sprite to do the same thing again and again. Instead of dragging a block twenty times, you use a loop. Loops are Control blocks (coloured gold/orange) that repeat the blocks placed inside them.

The repeat Loop

The repeat 10 block runs the blocks inside it a set number of times, then stops. If you want a sprite to move and turn to draw a square, you could repeat 4 with a move and a turn inside.

The forever Loop

The forever block repeats the blocks inside it without ever stopping, until you stop the project. It is perfect for things that should always keep checking or moving, like a sprite that always follows the mouse. Notice that a forever loop has no bump at the bottom, because nothing can come after it.

The repeat until Loop

The repeat until block keeps repeating until something becomes true. For example, repeat until touching edge will move a sprite over and over until it hits the edge, then move on.

Why Loops Are Great

  • They save you from copying the same blocks many times.
  • They make your scripts shorter and easier to read.
  • They are perfect for animation, because you can loop next costume.

A helpful tip: put a short wait 1 seconds block inside a fast loop. Without a wait, a loop can run so quickly that you cannot see what is happening!

Remember

  • repeat runs a set number of times.
  • forever never stops until you stop the project.
  • repeat until stops when a condition becomes true.

Stuck on this topic? A verified JomKelas tutor can walk you through it.

Find a verified tutor