# 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.

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.

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.

### 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.

## Discussion

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