Key Stage 1 computing · Algorithms and programming
KS1 debugging: finding and fixing bugs in simple programs
A bug is a mistake in a program, and debugging means finding it and fixing it. In Key Stage 1, children learn that programs rarely work first time, that the device did exactly what it was told, and that a calm, step-by-step search will find the problem. It builds patience, careful checking and the habit of explaining why something happened.
Debugging simple programs, explained
When a program doesn’t do what was intended, we say it has a bug. Debugging is the job of finding the mistake and putting it right. The programme of study names it directly: children are taught to create and debug simple programs. It is a normal part of programming at every level, including for adults who write software for a living, and teachers want children to see it that way.
In Key Stage 1, the bugs are usually small and easy to see once you know where to look. A command is in the wrong place, a step is missing, a number is wrong, or a turn goes the wrong way. With blocks, the problem may be a block that is not joined on. Teachers often set “bug hunts”, giving children a program that has a deliberate fault and a picture of what it should do.
There is a method, and children can learn it. First say what the program should do. Then run it, or trace it by following each command with a finger or counter, and find the first place where it goes differently from the plan. Change one thing there, and run it again. Changing only one thing at a time is important: if you change three things and it works, you don’t know which change mattered.
Debugging depends on reasoning. To spot a bug you have to know what each command does and what should happen next, which means predicting the result before you check it. That is why debugging and prediction are taught side by side, and why children are asked to explain a bug with a “because” sentence: “It went the wrong way because I used Left instead of Right.”
How adults respond matters. If a bug is treated as a failure, children can give up or ask to be shown the answer. If it is treated as a puzzle, they get better at persisting. When your child gets stuck, ask a question rather than pointing to the mistake: “Where was the last place it did the right thing?”
Words your child will hear
- Bug
- A mistake in a program that stops it doing what was intended.
- Debug
- To find a bug and fix it.
- Test
- To run a program and see whether it does what was planned.
- Trace
- To follow a program one command at a time, often with a finger or counter, to see what each one does.
- Predict
- To say what will happen before you run the program.
- Fix
- To change the program so the mistake is gone.
- Run
- To make the program start and carry out its commands.
What the National Curriculum says
In Key Stage 1, pupils should be taught to:
- Create and debug simple programs
- Use logical reasoning to predict the behaviour of simple programs
Objectives from the National Curriculum in England, © Crown copyright, used under the Open Government Licence v3.0.
Try it: finding the bug
A floor robot starts on the bottom-left square of a grid, facing up the mat. It should reach the flag, which is two squares up and two squares to the right. Forward moves one square, and Left and Right are quarter turns on the spot. The program is: Forward, Forward, Left, Forward, Forward. Instead of reaching the flag, the robot drives off the left-hand edge of the mat. Find the bug and fix it.
- Say the goal in words: go up two squares, turn to face the right, then go two squares. Compare it with the program, one command at a time.
- Trace the program with a counter. Forward, Forward: the counter moves two squares up the mat. So far this matches the plan.
Forward OK, 1 up Forward OK, 2 up
- Next command: Left. The robot should face right at this point, but Left turns it the other way. This is the first place where the program and the plan disagree, so the bug is here.
Left WRONG, now faces left
- Fix one thing only: change Left to Right. Leave the other commands alone.
Forward Forward Right Forward Forward
- Test again. The robot goes up two squares, turns to face the right and moves two squares along, landing on the flag. Say what you changed and why: the robot needed to face right, not left.
The bug was Left in the third command. Changing it to Right makes the program work: Forward, Forward, Right, Forward, Forward. The robot lands on the flag.
Common mistakes and how to help
Thinking the robot or computer made the mistake
When something goes wrong, it is natural to blame the machine, and the idea that a device can be “broken” is familiar to children.
What helps: Ask, “Did it do what we told it?” Nearly always the answer is yes, and then the question becomes, “What did we tell it?”
Deleting the whole program and starting again
Starting afresh feels easier than working out what went wrong, and children don’t yet see that most of the program is usually right.
What helps: Ask which commands were correct. Trace the program and mark the last correct step, so the child sees that only one part needs changing.
Fixing the end result instead of the cause
If the robot ends up two squares away from the target, children add extra commands to move it, which hides the bug and creates new problems.
What helps: Trace from the start and find the first command that differs from the plan. Fix that one and the later ones often fall into place.
Changing several things at once
Children want it to work quickly, so they alter many commands and then can’t tell which change helped.
What helps: Make a rule: one change, then test. Let your child pick which change to try first, and say what they expect to happen.
How to help at home
- Make a bug for them to hunt. Give your child a set of instructions with one mistake in it, such as the steps for making a paper boat with two steps swapped over. Ask them to find the first step that is wrong and explain why.
- Trace with a counter. Draw a small grid on paper and read out commands. Your child moves a counter one command at a time. This slow, careful tracing is exactly how teachers expect them to find bugs.
- Ask questions instead of pointing. When something does not work, ask, “What should it have done?”, “What did it do?” and “Where did it first go differently?” You don’t need to know the answer, only to ask the questions.
- Share your own mistakes. Mention when you take a wrong turn while driving or follow a recipe incorrectly, and say how you worked out the fix. Seeing adults debug calmly helps children see mistakes as ordinary.
- Celebrate the find. Praise the moment your child spots a bug, not only when the program works. “You found it” is worth as much as “you got it right”, because finding it is the hard part.
Related topics
Leads on to
- Key Stage 2: Designing, writing and debugging programs
- Key Stage 2: Controlling and simulating physical systems
- Key Stage 2: Decomposition: breaking problems down
- Key Stage 2: Sequence in programs
- Key Stage 2: Selection: if and else
- Key Stage 2: Repetition and loops
- Key Stage 2: Variables, input and output
- Key Stage 2: Explaining how algorithms work
- Key Stage 2: Debugging algorithms and programs
More on algorithms and programming
- Key Stage 1: From algorithms to programs
- Key Stage 1: Creating simple programs
- Key Stage 1: Predicting what a program does
- Key Stage 1: What is an algorithm?
Updated .