KeyStage

Key Stage 2 computing · Algorithms and programming

KS2 loops in coding: repeating instructions without rewriting

Repetition means making a program do the same thing more than once without writing it out again and again. In coding, this is done with a loop. In Key Stage 2, children learn to use loops that repeat a set number of times, loops that go on forever and loops that repeat until something happens, and to spot where a pattern in a sequence could be replaced by a loop.

Repetition and loops, explained

Repetition is the third building block named in the Key Stage 2 programme of study, alongside sequence and selection. Anyone who has asked a child to move forward four steps by saying “forward, forward, forward, forward” has already seen the problem. Loops let the program say “repeat 4 times: forward” instead. They make programs shorter, easier to read and, importantly, much easier to change: altering one number in a loop does the work of editing many separate lines.

Children usually meet three kinds of loop. A count-controlled loop repeats a set number of times, such as “repeat 10”. A forever loop (sometimes called an infinite loop) repeats until the program is stopped, and is common in games where something must be checked all the time, such as whether a key is pressed. A condition-controlled loop repeats until a condition becomes true, such as “repeat until the score is 20”. Which tool and which words a school uses is its own choice, but the ideas are the same everywhere.

A key skill is spotting patterns. Given a long sequence such as “move 100, turn 90, move 100, turn 90, move 100, turn 90, move 100, turn 90”, children learn to notice that “move 100, turn 90” appears four times, and to rewrite it as a loop. This is a form of the logical reasoning the curriculum asks for, and it helps with decomposition because it shows which part of the problem is the repeating unit. Children must also decide what belongs inside the loop and what belongs outside it: instructions that should happen once, such as setting the score to 0 at the start of a game, stay outside.

Loops also combine with selection and variables. A loop might repeat “if the sprite touches the edge, turn around” to keep a character on screen, or add 1 to a counter each time round. In Years 5 and 6 pupils may place one loop inside another, for example to draw a row of shapes. Debugging loops involves a particular kind of tracing: writing out what happens on each pass, with the values of any variables. Common errors are repeating the wrong number of times and putting the wrong instructions inside the loop.

At home, loops are everywhere: brushing teeth, stirring, skipping, a drum beat, a pattern in a bracelet. Pointing them out with “that’s a loop” helps children to see that repetition is a way of describing the world.

Words your child will hear

Repetition
Doing the same instructions again and again.
Loop
A part of a program that repeats instructions.
Count-controlled loop
A loop that repeats a set number of times, such as repeat 4.
Forever loop
A loop that carries on repeating until the program is stopped.
Condition-controlled loop
A loop that repeats until something becomes true, such as until the score is 20.
Iteration
One go round a loop. Repeat 4 means four iterations.
Pattern
A set of instructions that appears more than once, which could be turned into a loop.

What the National Curriculum says

In Key Stage 2, pupils should be taught to:

  • Use sequence, selection, and repetition in programs; work with variables and various forms of input and output
  • Use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

Objectives from the National Curriculum in England, © Crown copyright, used under the Open Government Licence v3.0.

Try it: drawing shapes with a loop

A child is using a program that draws a line with the instruction “move 100” and turns with the instruction “turn right 90”. The program for a square is: move 100, turn right 90, move 100, turn right 90, move 100, turn right 90, move 100, turn right 90. How can a loop shorten it, and how can the same loop draw a hexagon?

  1. Look for the repeating pattern. The pair “move 100, turn right 90” appears four times in a row. That pair is the part that goes inside the loop.
  2. Write the loop. Count the repeats: four sides, so repeat 4.
    repeat 4
      move 100
      turn right 90
  3. Check by tracing. Pass 1 draws the first side and turns a quarter turn. Passes 2, 3 and 4 do the same. After four passes the line has returned to its start and the square is closed.
  4. Adapt it for a hexagon. A hexagon has six sides, so the loop repeats 6 times. The turn at each corner is one sixth of a full turn of 360 degrees, which is 60 degrees. This step uses angle facts children usually meet in upper Key Stage 2; younger pupils often find the turn by trying an angle and checking whether the shape closes. Only two numbers change.
  5. The new program is shorter than writing out twelve instructions.
    repeat 6
      move 100
      turn right 60

The square is repeat 4 [move 100, turn right 90]. The hexagon is repeat 6 [move 100, turn right 60], because 360 ÷ 6 = 60. Changing two numbers in the loop is much easier than rewriting twelve lines.

Common mistakes and how to help

Thinking a loop is only a way to save typing

Shorter programs are the first benefit children notice, so they think that is all a loop does.

What helps: Show how a loop makes a program easier to change: altering one number changes how many times the instructions run, with no lines to add or delete by hand. Loops also make programs clearer to read, because the repeating unit is easy to see.

Putting the wrong instructions inside the loop

Everything that looks related gets dragged in. If “set score to 0” sits inside a loop, the score resets on every pass.

What helps: Ask of each instruction, “Should this happen every time round, or just once?” Things that happen once, like starting values, belong outside.

Miscounting the repeats

“Repeat 4” is read as “do it once, then repeat it four times”, giving five goes. Or the number is chosen without counting the actual sides.

What helps: Trace the loop on paper, ticking each pass. Remember that repeat 4 means four goes in total.

Using the shape’s inside angle as the turn

For a square the inside angle and the turn are both 90, so the error is hidden. For a hexagon the inside angle is 120, but the turn at each corner is 60.

What helps: Walk the shape physically: when you go round a hexagon, you turn through a total of one full turn (360 degrees) in 6 equal turns. Draw it with the wrong angle and see what appears.

How to help at home

  • Find the loops. Notice repeating actions through the day: stirring, brushing teeth, climbing stairs, a drum beat. Ask, “How many times? Is it counted, or does it go on until something happens?”
  • Write a dance as a loop. Invent a short dance, such as clap, stamp, spin, and write it as “repeat 3 times: clap, stamp, spin”. Perform it, then change the number of repeats. You have just edited a program.
  • Shorten a long list. Write out a long sequence of instructions with a clear pattern, such as a stack of drawing moves, and ask your child to rewrite it with a loop. Check that both versions give the same result.
  • Walk a shape. In a garden or large room, walk a square and then a triangle, taking the same number of steps on each side and turning at each corner. Ask how many turns, and how big they were. Then try writing it as a loop.
  • Spot the stop. Ask, “When does this loop stop?” about a game or a routine. A loop with no way of stopping is a bug, unless it is meant to run forever.

Leads on to

Everything in Key Stage 2 computing

Updated .