Loops and Events in Block Code

19 min
0/3 practice checks

Repeating with loops

In block-coding tools like Scratch, you build a program by snapping coloured blocks together. Two of the most useful ideas are loops and events.

A loop repeats a set of blocks so you do not have to copy them over and over. Instead of writing 'move forward' four times, you wrap it in a loop:

repeat 4 times {
    move forward 10 steps
    turn right
}

This makes the sprite trace a square: forward and turn, four times. The blocks inside the loop run again and again until the count is finished.

Starting with events

An event is something that happens, and it starts a piece of code running. Event blocks usually begin with the word when:

  • when green flag clicked - start the program
  • when space key pressed - do something on a key press
  • when this sprite clicked - react to a mouse click

Events let a program wait quietly and then react. Nothing runs until the event happens. A game might say: when green flag clicked, forever move the ball; when space key pressed, make it jump.

Which block is a loop - the block whose job is to repeat other blocks?

A sprite runs this program:

repeat 5 times {
    move forward 10 steps
    beep
}

How many times does the sprite beep in total?

You want a game to start moving the ball only when the player clicks the green flag. Which block should sit at the very top of that code?