Repetition

What is repetition?

Repetition is the recurrence of actions or events. For example, we eat our lunch each lunchtime and might watch a favourite soap every evening. Repetition in programming means to repeat the execution of certain instructions. This can make a long sequence of instructions much shorter, and, typically, easier to understand. Spotting repeatable instructions within an algorithm to implement within a program draws on the computational-thinking processes of pattern recognition and generalisation.

The Bee-Bot program for moving around a square is “forward, left, forward, left, forward, left, forward, left”: for each side, the Bee-Bot moves forward and then turns left. A Roamer-Too or Pro-Bot enables the equivalent coding to be simplified with the built-in Repeat command: “repeat 4 [forward, left]”. Similarly, the two sets of Logo code below both draw an equilateral triangle, with one utilising the Repeat command:

A sequence of code in the Logo programming language.

Two blocks of code in the Logo programming language.

Compare the two blocks of code above. The repeated code is run a fixed number of times, which is the best way to introduce repetition. You can also repeat code forever. This can be useful in ‘real world’ systems, such as a control program for a digital thermostat, which should continually check the temperature of a room, sending a signal to turn the heating on when this drops below a certain value. This is a common technique in game programming. For example, the following Scratch code would make a sprite continually chase the mouse pointer around the screen:

'A block of "Repeat forever" code in Scratch.

A block of "Repeat forever" code in Scratch.

Repetition can be combined with selection so that a repeating block of code is run as many times as necessary until a certain condition is met, as in this fragment in Scratch:

A block of code in Scratch to repeat instructions until a condition is met.

A block of code in Scratch to repeat instructions until a condition is met.

A repeating block of code is sometimes referred to as a loop. It’s possible to “nest” one loop inside another – the crystal-flower programs in Logo use this idea:

You can nest one repeating block of code inside another. The crystal-flower patterns in Logo use this idea.

Why is repetition important?

Many programs employ repetition. In electronic games and ‘real world’ control programs, blocks of code loop indefinitely. A sliding door continually monitors for movement in front of it and sends a signal to a motor to open it when this is detected.

Employing repetition enables us to create programs which can be easier to alter if required, because the repeated code needs to be altered only once. For example, the screenshots below show two programs used to draw a triangle in Scratch, one of which employs a count-controlled loop. By using this loop command, we need only alter one value (circled), as opposed to three, in order to alter the size of the triangle which the programs draw. Indeed, the count-controlled loop program can quickly be modified to draw an entirely different, regular polygon by simply changing the number of times the program is set to repeat and the angle through which the sprite should rotate:

Two blocks of Scratch code for a triangle, but one block uses repetition and is much simpler to use.

Two blocks of Scratch code for a triangle. The code using repetition (on the right) can quickly be altered to draw a larger triangle, just by changing the one circled value.

What does repetition look like in the Primary curriculum?

Whilst the use of repetition within programs isn't specifically mentioned until Key Stage 2, throughout EYFS and Key Stage 1, pupils become aware of repetition in their daily routines and in the world around them. During this period, some programming languages provide simple repetition commands which can be used when coding. Key Stage 2 pupils may recognise that, when coding, they're using the same commands over and over for the same purpose - e.g. to reduce the number of remaining game lives by one every time they collide with an alien. Pupils may use the More Blocks category in Scratch 2 to create a procedure for this. As they create plans and algorithms for their programs, they should start to recognise the type of repetition command which will enable them to code a program as required. Pupils use a broader range of repetition commands; including "forever", "condition-controlled" and "fixed-number". They might also look at combining (nesting) these to achieve a desired outcome. On a broader level, they might make use of Backpack feature in the online version of Scratch, to store code which they find themselves repeating many times across several projects.

3 - 5 years

Introduce pupils to the repetition in the world around them, such as the perpetual cycle of day and night or the things that sometimes repeat, such as coming to school Monday through to Friday. The school day itself has a repeating pattern of times. Pupils learn to hang up their coat at the start of the day, to line-up in an orderly fashion at the end and to repeat these routines on a daily basis. Many nursery rhymes have repeating phrases, and songs have repetition in their structures. As pupils learn to count beyond 10, they may recognise the repeating pattern in the digits.