KeyStage

Key Stage 2 computing · Algorithms and programming

KS2 sequence in coding: why the order of instructions matters

Sequence means that a program carries out its instructions one after another, in the order they are written. It is the simplest of the three building blocks of programming (sequence, selection and repetition), but it is the one that everything else rests on. In Key Stage 2, children learn to plan, predict and fix sequences, including ones that use variables and timing, and to explain what a program will do before they run it.

Sequence in programs, explained

The Key Stage 2 programme of study asks pupils to use sequence, selection and repetition in programs, to work with variables and various forms of input and output, and to use logical reasoning to explain how some simple algorithms work and to detect and correct errors. Sequence comes first because every program is, at heart, a list of instructions. A computer follows them in order, from the top to the bottom, one at a time, and does exactly what is written, never what was meant.

Many children first meet sequence in Key Stage 1, for example when they give a floor robot a route. In Key Stage 2 the sequences get longer and richer. A program might say something, wait, move, change costume, play a sound and then say something else. Children learn that changing the order changes the result, and that timing instructions such as “wait 2 seconds” are part of the sequence. In many block-based tools, if a character says “Hello” and then says “Goodbye” straight away, the first message is replaced so quickly that nobody sees it. Adding a wait between the two fixes the sequence.

A key skill is tracing: reading a program line by line, acting as the computer, and keeping track of what changes as you go. Teachers often ask pupils to predict the outcome before they press run, then compare. When programs use variables, tracing means writing down the value of each variable after each instruction, in a small table. This is the logical reasoning the curriculum describes, and it is the best way to find an error in a sequence: the table shows where the value goes wrong.

Sequences also show the difference between setting a variable and changing it. “Set score to 5” puts the number 5 into the score, whatever it was before. “Change score by 5” adds 5 to whatever it is already. Many bugs in children’s early games come from mixing the two up. In some tools several scripts can run at the same time, for example when different keys are pressed, but each one still runs its own instructions in order, from the top.

As a parent, you can help by asking your child to explain a program in order: “What happens first? What happens next?” If the explanation does not match what happens on the screen, you have found a bug together.

Words your child will hear

Sequence
Instructions carried out one after another, in order.
Instruction
A single command that tells a computer what to do.
Trace
To follow a program step by step, noting what changes, as the computer would.
Variable
A named store for a value that can change, such as a score.
Predict
To work out what a program will do before it runs.
Output
What a program shows or does, such as a message on screen or a sound.
Run
To start a program so the computer follows its instructions.

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: tracing a sequence

A program has these instructions in this order: (1) set total to 4, (2) change total by 3, (3) set total to total × 2, (4) say total. What does it say? What would it say if instructions 2 and 3 swapped places?

  1. Trace the original order, noting the value of total after each instruction.
    1. set total to 4      total = 4
    2. change total by 3   total = 7
    3. set total to total × 2
                           total = 14
    4. say total          says 14
  2. Check each step. Instruction 2 adds 3 to 4 to make 7. Instruction 3 doubles 7 to make 14. Instruction 4 then says the current value of total, which is 14.
  3. Now swap instructions 2 and 3 and trace again.
    1. set total to 4      total = 4
    2. set total to total × 2
                           total = 8
    3. change total by 3   total = 11
    4. say total          says 11
  4. Compare. The same four instructions in a different order give 11 instead of 14. The computer cannot tell what was meant: it only follows the order.
  5. Finally, think about instruction 4 on its own. If “say total” were the first instruction, the program would not say 14, because total would not yet have been given its value (or would still hold an old one). Instructions that use a value must come after the instructions that set it.

The program says 14. With instructions 2 and 3 swapped it says 11, because (4 × 2) + 3 = 11, whereas (4 + 3) × 2 = 14. The order of a sequence changes the result.

Common mistakes and how to help

Expecting the computer to work out what was meant

People fill gaps for each other all the time, so children assume a computer will too. It will not: it follows each instruction exactly.

What helps: Play the literal robot at home. Follow your child’s instructions exactly as they are given, such as “put the jam on the bread” when the jar is still closed, and let them see why precision matters.

Thinking order only matters for movement

Early lessons use floor robots, where the order of turns and moves is obvious. Children do not see that the order of other instructions matters too.

What helps: Use the trace table to show a calculation changing with the order, as in the example. Try it with messages, sounds and changes to costume too.

Mixing up “set” and “change”

Both blocks involve a variable and a number, and the difference is small to read but large in effect. “Set score to 1” resets the score each time, while “change score by 1” adds to it.

What helps: Trace a score through three goals using each instruction and compare the tables. Ask, “Do you want to start again from this number, or add to what is there?”

Forgetting that a computer is fast

A child sees “say Hello” followed by “say Goodbye” and expects to see both, but the computer finishes the first long before the child can read it.

What helps: Suggest adding a wait instruction between steps, then test with different times. It shows why timing is part of the sequence.

How to help at home

  • Be the literal robot. Ask your child to give you instructions for making a sandwich or drawing a simple picture. Follow them exactly, including any missing steps, and enjoy the results. Then let them rewrite the sequence.
  • Trace on paper. Write four short instructions that change a number, such as start at 10, add 4, halve it, then add 3. Ask your child to predict the result, then trace it to check (10, 14, 7, 10). Swap the middle two instructions and trace again (10, 5, 9, 12).
  • Order the jumbled steps. Write the steps of a routine, such as getting ready for school, on separate bits of paper. Mix them up and ask your child to put them in an order that works, then explain which steps must come before others.
  • Predict before pressing run. When your child shows you a program, ask them to say what will happen before they run it. Each correct prediction is evidence of understanding, and each surprise is a bug to explore.
  • Dance a sequence. Make up a short sequence of moves, such as clap, jump, turn, and ask your child to perform it in order, then in a changed order. It shows that order changes the outcome, and there is no screen involved.

Leads on to

Everything in Key Stage 2 computing

Updated .