Home / Reasoning / Find the Bug: Trace, Spot the Error and Fix the Program
Reasoning & Coding · Coding
Find the Bug: Trace, Spot the Error and Fix the Program
Practise debugging: trace a program step by step, compare it with what it should do, find off-by-one, wrong-start and wrong-test bugs, and choose the right fix.
The idea
A bug is a mistake in a program. Debugging is the careful work of finding the bug and fixing it. Good programmers do not guess; they trace.
Step 1: write down what the program should do for a small example. Step 2: trace what it actually does, one step at a time, keeping track of every variable. Step 3: find the first step where the trace stops matching your plan. That step is where the bug lives.
Common bugs are easy to name. Off by one: the loop stops one step early or one step late. Wrong start: a counter, total or biggest begins at the wrong value. Wrong test: the program uses > when it needs >=, or odd when it needs even. Missing update: nothing inside a loop moves it toward stopping. Wrong order: a step happens before the thing it depends on.
Fix the rule, not the example. Changing 9 into the answer 9 makes one test pass but breaks every other list. After a fix, test with a small, a boundary and an unusual input (such as negative numbers).
Worked examples
Example 1 · Trace to find the bug
Program: total = 10; For each n in [2, 3, 5] [total = total + n]; say total. It should say the sum of the list. What is wrong?
- What should happen: 2 + 3 + 5 = 10.
- Trace: total starts at 10; 12; 15; 20. The program says 20.
- The first step that does not match the plan is the start: total should begin at 0.
Wrong starting value. Set total = 0.
Example 2 · Off by one
Program: count = 0; Repeat until count > 5 [count = count + 1; say count]. It should say 1 to 5. What happens?
- Trace: count becomes 1, 2, 3, 4, 5 and each is said.
- After 5, count > 5 is still false, so the loop runs once more and says 6.
- Stop when count == 5 so the loop ends right after saying 5.
It says 1 to 6. Change count > 5 to count == 5.
Example 3 · Wrong comparison
Program: biggest = 0; For each n in [7, 2, 9, 4] [If n < biggest [biggest = n]]; say biggest. It says 0. Why?
- Trace: 7 < 0 is false; 2 < 0 is false; 9 < 0 is false; 4 < 0 is false.
- biggest never changes, so it stays 0.
- To find the biggest, replace biggest when n is greater: n > biggest.
The comparison is backwards. Use n > biggest; the program then says 9.
Common mistakes
Why this fails: A random change can fix one example and break every other. Trace first so you know which step is wrong.
Why this fails: This passes one test and fails all others. A fix must work for every list, not only the one in front of you.
Why this fails: Bugs often hide at boundaries (exactly 50, an empty list, negative numbers). Test small, boundary and unusual inputs.
Why this fails: Find the first step where the trace stops matching your plan. Later wrong values are often only a result of that first mistake.
Why this fails: If nothing inside a loop moves the loop towards stopping, it can run forever. Check that the loop changes what the test looks at.
Foundation
Foundation practice
What a bug is, and how to trace.
1. In programming, what is a bug?
- A tiny animal inside the computer
- A button that starts the program
- A mistake in a program that makes it give the wrong result
- A word that is spelled correctly
2. Program: x = 4; x = x + 2; x = x * 3; say x. What does it say?
- 18
- 10
- 14
- 9
3. Program: x = 10; Repeat 3 times [x = x - 2]; say x. What does it say?
- 8
- 4
- 6
- 2
4. A program should say 1, 2, 3, 4, 5 but says 1, 2, 3, 4. Where should you look first?
- The title of the program
- The first number in the list
- The colour of the screen
- The stopping condition of the loop
5. A program gives a wrong answer. What is the best first step?
- Delete the whole program
- Trace it with a small test and compare each step to what you expect
- Change numbers at random until it works
- Run it again and hope it changes
Practice
Practice practice
Name the bug and choose the fix.
6. Program: count = 0; Repeat until count > 5 [count = count + 1; say count]. It should say 1 to 5 only. Which fix works?
- Change count + 1 to count + 2
- Change count = 0 to count = 10
- Change count > 5 to count == 5
- Remove the say step
7. Program: total = 10; For each n in [2, 3, 5] [total = total + n]; say total. It should add the list, 10. It says 20. What is the bug?
- total should start at 0, not 10
- The list is the wrong length
- total + n should be total - n
- There should be a say step before the loop
8. Program: For each n in [1, 2, 3] [say n + 2]. It should double each number: 2, 4, 6. It says 3, 4, 5. Which fix works?
- Change n + 2 to n + 3
- Change n + 2 to n * 2
- Change the list to [2, 4, 6]
- Change say to repeat
9. Program: biggest = 0; For each n in [7, 2, 9, 4] [If n < biggest [biggest = n]]; say biggest. It says 0, not 9. Which fix works?
- Change biggest = 0 to biggest = 100
- Change [biggest = n] to [biggest = 9]
- Change n < biggest to n > biggest
- Change say to repeat
10. A pass mark is 50 or higher. Program: If mark > 50 [say "Pass"] Else [say "Try again"]. A student with exactly 50 gets "Try again". Which fix works?
- Change 50 to 5
- Change Else to If
- Change "Pass" to "Fail"
- Change > to >=
11. Program: x = 1; Repeat until x > 3 [say x]. It never stops. What is missing?
- A second say step
- A step inside the loop that changes x, such as x = x + 1
- A different starting number
- An Else branch
Think
Think practice
Fix counters, tests, order and starting values.
12. Program: count = 0; For each n in [4, 9, 12, 7, 15] [If n > 8 [count = 1]]; say count. It should count the numbers over 8 (answer 3) but says 1. What is the fix?
- Change count = 1 to count = count + 1
- Change count = 0 to count = 3
- Change n > 8 to n < 8
- Remove the If
13. Program: total = 0; For each n in [2, 5, 6, 9, 10] [If n is odd [total = total + n]]; say total. It should add the even numbers (18) but says 14. What is the fix?
- Change total = 0 to total = 4
- Change odd to even
- Change [2, 5, 6, 9, 10] to [2, 6, 10]
- Change + to *
14. Program: x = 5; say x; x = x + 1. It should say the new value, 6. Which fix works?
- Change x = 5 to x = 6
- Change say to repeat
- Move say x to the end, after x = x + 1
- Change + to -
15. Program: biggest = 0; For each n in [-5, -2, -9] [If n > biggest [biggest = n]]; say biggest. It says 0, but the biggest number in the list is -2. What is the best fix?
- Start biggest as the first item in the list (-5)
- Start biggest at 100
- Change n > biggest to n < biggest
- Say 0 only when the list has negative numbers
Challenge
Challenge practice
Explain a bug in your own words.
16. Program: smallest = 0; For each n in [6, 3, 8] [If n < smallest [smallest = n]]; say smallest. It says 0 but should say 3. Explain the bug and write the fix.
Answer key
- 1. A mistake in a program that makes it give the wrong result
A bug is an error in the steps or the rules of a program. Debugging means finding the bug and fixing it. - 2. 18
Follow the steps in order: x = 4, then x = 6, then x = 18. 10 and 14 skip or change a step, and 9 multiplies by 3 before adding. - 3. 4
Each pass takes away 2: 10 → 8 → 6 → 4. Saying 8 or 6 stops early, and 2 repeats the loop five times. - 4. The stopping condition of the loop
The program stops one step too early, so the test that ends the loop is the most likely place for the bug. This kind of mistake is called an off-by-one error. - 5. Trace it with a small test and compare each step to what you expect
Tracing shows the exact step where the program stops matching your plan. Random changes can hide a bug or create a new one. - 6. Change count > 5 to count == 5
With count > 5 the loop continues until count is 6, so it says 6 as well. Stopping when count == 5 gives 1, 2, 3, 4, 5. The other changes skip numbers, never start, or say nothing. - 7. total should start at 0, not 10
Starting at 10 adds 10 to the real sum (2 + 3 + 5 = 10), so the answer is 20. Starting at 0 gives 10. Subtracting would give 0. - 8. Change n + 2 to n * 2
Adding 2 and doubling agree only when n is 2, which hides the bug. Multiplying by 2 gives 2, 4, 6 for every number. Changing the list or the command does not fix the rule. - 9. Change n < biggest to n > biggest
No number is below 0, so biggest never changes. Using > replaces biggest each time a larger number appears, ending with 9. Starting at 100 would never change either, and hard-coding 9 only works for this list. - 10. Change > to >=
The word "or higher" includes 50, but > leaves it out. >= includes 50. Changing the number or the words does not fix the boundary. - 11. A step inside the loop that changes x, such as x = x + 1
The loop stops when x > 3, but x never changes, so the test is never true. Adding x = x + 1 inside the loop moves x toward the stopping value. - 12. Change count = 1 to count = count + 1
count = 1 sets the counter to 1 each time instead of adding one more. count = count + 1 gives 1, 2, 3 for 9, 12 and 15. Setting count to 3 at the start is hard-coding the answer. - 13. Change odd to even
The test picks 5 and 9 (total 14). Even picks 2, 6 and 10, which adds to 18. Changing the start only guesses, and changing the list hides the bug instead of fixing the test. - 14. Move say x to the end, after x = x + 1
The program says x before it changes, so it says 5. Putting say after the update gives 6. Starting at 6 would make the answer 7 after the add. - 15. Start biggest as the first item in the list (-5)
Every number in the list is below 0, so 0 never gets replaced. Starting with a real item from the list works for any numbers. Starting at 100 would be just as wrong, and switching the comparison would find the smallest. - 16. The program starts with smallest = 0, which is lower than every number in the list, so nothing replaces it. Fix: start with smallest = 6 (the first item), then compare the rest.
Tracing: 6 < 0 is false, 3 < 0 is false, 8 < 0 is false, so smallest stays 0. Starting from the first item means 3 < 6 is true and smallest becomes 3. A good check is to test a list where the answer is not 0.