Skip main navigation

Repetition in Scratch

Scratch includes three `repeat` blocks which allow you to create loops, depending on how you want the loop to run. Watch Vasu put them into practice.

In programming, using a command to repeat a set of instructions is called a ‘loop’; all programming languages include ways to do this. Scratch includes three repeat blocks which allow you to create loops, depending on how you want the loop to run. You will find these repeat blocks in the Control category of the Blocks menu.

Looping forever

You might want a repeating sequence of actions to run continuously whilst your program is running. A good example of this idea is a set of traffic lights which show a repeating pattern of coloured lights without stopping.

To do this, Scratch provides you with the forever block which allows you to repeat a sequence of commands forever. The sequence of commands you place inside the block will be executed continually by your computer until you stop your program.

Scratch forever block.

Note: Because this type of loop has no end, you can’t added any more code after it.

Looping a set number of times

Some loops need to repeat a specific number of times. To automate packing eggs into egg boxes which contain 6 eggs each, you would need to repeat the process of picking up an egg and placing it in the box 6 times.

To create a loop like this in Scratch, you would need to use the repeat (number) block. This allows you to repeat a sequence of commands a particular number of times; you set the number of times the loop will repeat at the top of the block. In programming, this type of loop is called a ‘count-controlled’ loop.

Scratch `repeat ()` block

Note: When you use a repeat (number) loop you can add code after it. After the loop has run for the given number of times, then the code after the loop will run.

Conditional loop

Sometimes you will not know how many times you want an action to repeat — it might depend on the circumstances. For example, a set of automatic windscreen wipers on a car will wipe one way, then the other continually until it’s not raining.

The third type of Scratch repeat block, the repeat until block, provides a similar function to this. This block checks at the start of each loop to see if a condition is met and if the condition is met, it stops looping. Later in the course I will introduce you to the programming concept of ‘conditions’ and we will explore this block at that point.

Note: When you use a repeat until loop, you can add code after it. The code after the loop will run once the repeat until block has found that the condition was met.

Scratch `repeat until` block.

Adding blocks to a loop

To add blocks to a loop in Scratch, you should drag them into the loop block. A grey shadow will appear indicating where your block will snap in. When the shadow is where you want your new block to be, release the block.

Screen recording showing a 'move 10' block being dragged inside a 'repeat 10' loop block. The block is released and it snaps inside the loop. A 'turn right 15 degrees' block is then dragged into the same loop and snapped to the bottom of the move block.

Discussion

In the comments section, share examples of some real-life scenarios where the different types of repetition occur.

This article is from the free online

Introduction to Programming with Scratch

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education