Key Stage 2 computing · Algorithms and programming
KS2 debugging: finding and fixing errors in programs
A bug is a mistake in an algorithm or a program that makes it behave differently from what was intended. In Key Stage 2, children are expected to detect and correct errors in both algorithms and programs, using logical reasoning rather than guesswork. Debugging is a calm, step-by-step habit, and it is one of the most useful things a child learns in computing.
Debugging algorithms and programs, explained
Children are taught that bugs are normal. Professional programmers spend a large part of their time finding and fixing them, so a program that doesn’t work first time is not a sign that a child has failed. A good teacher will praise the moment a child says “That’s not what I expected”, because noticing the gap between what you meant and what happened is where debugging starts.
The process usually runs in a loop. First, the child says what the program should do. Then they run it and look carefully at what it actually does. Next comes the detective work: reading the code line by line, tracing it on paper with real values, or running it a small piece at a time to see where it first goes wrong. Once the problem is located, they change one thing, test again, and check that the fix hasn’t broken something else.
Errors come in a few kinds. Some are slips, such as a wrong number, a misspelled word in a text language or a block in the wrong place. Others are logic errors, where every instruction is valid but the plan is wrong, such as turning the wrong angle, repeating too few times or checking the wrong condition. Algorithms written on paper can have bugs too: a missing step, steps in the wrong order, or an instruction that could be read two ways.
Debugging is closely tied to the other skills in this strand. Decomposing a program into small parts makes a bug easier to pin down, because each part can be tested separately. Predicting what a program will do before running it, and then comparing, trains the same reasoning. Many schools also have children debug each other’s code in pairs, explaining their thinking out loud, which often helps the author spot the problem.
Words your child will hear
- Bug
- A mistake in an algorithm or program that makes it behave in an unexpected way.
- Debug
- To find and fix bugs.
- Test
- Run a program, or work through an algorithm, to see whether it does what was intended.
- Trace
- Follow a program step by step, writing down what changes, to find where it goes wrong.
- Logic error
- A mistake in the plan: the program runs, but it doesn’t do the right thing.
- Syntax error
- A mistake in how code is written, such as a typing slip, so the computer can’t follow it.
- Decompose
- Break a problem into smaller parts so each can be tested and fixed separately.
What the National Curriculum says
In Key Stage 2, pupils should be taught to:
- Use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs
- Design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts
Objectives from the National Curriculum in England, © Crown copyright, used under the Open Government Licence v3.0.
A worked example: a square that won’t close
A child writes this program to draw a square with a turtle-style robot or screen pointer. It draws a shape with a gap at the end instead of a square. Find and fix the bug.
- Say what should happen. A square has four equal sides and four equal turns, so the program should draw a side, turn, and repeat four times.
repeat 4 times move 100 steps turn 80 degrees
- Look at what actually happens. The pointer draws four sides of equal length, but the shape is lopsided and the last side finishes some way from the starting point, leaving a gap. The sides look right and the corners do not, so the bug is probably in the turn.
- Trace it. Each turn is 80 degrees, so after four turns the pointer has turned 4 × 80 = 320 degrees in total. To finish back where it started, facing the same way, it needs to turn a full 360 degrees.
Turns: 4 × 80 = 320 degrees Needed: 360 degrees 360 ÷ 4 = 90 degrees per turn
- Change one thing: set the turn to 90 degrees. Do not change the number of repeats or the side length, because they were already correct.
repeat 4 times move 100 steps turn 90 degrees
- Test again. The shape now closes. Then check it still works with a different side length such as 50 steps, to be sure the fix wasn’t a lucky one.
The bug was a logic error: the turn was 80 degrees instead of 90. Changing it to 90 degrees makes four turns add up to 360 degrees and closes the square.
Common mistakes and how to help
Changing lots of things at once
When a program fails, the urge is to fix everything in one go. If it then works, nobody knows which change mattered, and if it doesn’t, new bugs may have appeared.
What helps: Agree a rule at home: change one thing, run it, and look. It feels slower but it is the quicker route to the answer.
Believing the computer is wrong, not the program
The code looks right to its author, because they know what they meant. Computers follow the instructions exactly as given and cannot guess the intention.
What helps: Ask your child to read the code aloud as if they were the computer, doing only what each line says. The gap often appears quickly.
Looking only for typing slips
Block-based languages remove most spelling errors, but children still expect a bug to be a small slip. Logic errors, like a wrong angle or a wrong condition, are harder to spot.
What helps: Ask, “What did you want to happen? What happened instead?” and trace with real numbers. Only then ask which line caused the difference.
Thinking a program is fixed when it works once
A test with one input can pass by luck. The same program may fail with a different number, an empty answer or a second run.
What helps: Test with several different inputs, including awkward ones, such as the smallest, the biggest and zero, before calling it finished.
How to help at home
- Praise the bug. When your child’s program doesn’t work, ask them to tell you what they expected and what happened. Don’t take over. Staying curious teaches persistence, which matters far more than getting it right first time.
- Debug an everyday algorithm. Write out the steps for making a drink or a sandwich, with one deliberate mistake such as a missing step or a wrong order, and ask your child to find it. Or follow your child’s instructions literally and see where they go wrong.
- Be the rubber duck. Ask your child to explain their program to you line by line, even if you don’t understand the code. Putting it into words often makes them spot the problem unprompted. Programmers call this “rubber duck debugging”, because explaining code to anyone, even a toy duck, helps.
- Swap and check. With a sibling or friend, each write a short set of directions for a route around the house, then swap and follow each other’s exactly. Whoever ends up in the wrong place then works with the author to find which instruction caused it.
Related topics
Builds on
- Key Stage 1: What is an algorithm?
- Key Stage 1: From algorithms to programs
- Key Stage 1: Creating simple programs
- Key Stage 1: Debugging simple programs
- Key Stage 1: Predicting what a program does
Leads on to
- Key Stage 3: Searching algorithms
- Key Stage 3: Sorting algorithms
- Key Stage 3: Comparing algorithms
- Key Stage 3: Text-based programming
- Key Stage 3: Lists, tables and arrays
- Key Stage 3: Functions and procedures
More on algorithms and programming
- Key Stage 2: Controlling and simulating physical systems
- Key Stage 2: Decomposition: breaking problems down
- Key Stage 2: Designing, writing and debugging programs
- Key Stage 2: Explaining how algorithms work
- Key Stage 2: Repetition and loops
- Key Stage 2: Selection: if and else
- Key Stage 2: Sequence in programs
- Key Stage 2: Variables, input and output
Updated .