All free lessons / Computing / Debugging and logical reasoning
Computing · FSCE
Debugging and logical reasoning
A free preview from the LearnAhead Learning Library — the same lesson registered students learn from.
In this lesson: Find and fix the common bugs in short programs — wrong order, wrong counts, wrong conditions — by predicting output, testing systematically and reasoning step by step.
The idea
A bug is an error in a program; debugging is the detective work of finding and fixing it. Programs rarely fail randomly — they fail logically, doing exactly what the buggy instructions say. The debugger's mindset is: the computer is obedient; somewhere, what I wrote is not what I meant.
Bugs come in families. Sequence bugs: right steps, wrong order. Count bugs: a loop repeats 3 times instead of 4, or an angle is 60 where the shape needs 90. Condition bugs: 'if score < 10' where the design needs 'if score > 10', so the program rewards failure. Variable bugs: forgetting to set a starting value, or updating the wrong variable. Each family has a test that exposes it: trace the program with simple numbers and compare the result with what should happen.
Logical reasoning is the engine of debugging: predict before you run. Write down what each line should do, run a dry trace, and find the first line where prediction and behaviour part company — the bug lives at or before that line, never after it. FSCE debugging questions give you a program, its intended result and its actual result; your job is to identify the single change that fixes it.
Worked example
A program should draw a square: repeat 4 (forward 10, turn 90). Instead it draws an open shape like three sides of a square with a gap. The code reads: repeat 3 (forward 10, turn 90). What is the bug and the fix? A: The angle is wrong; change 90 to 120. B: The repeat count is wrong; change 3 to 4. C: The forward distance is wrong; change 10 to 40. D: There is no bug; squares have three sides.
- Three repeats draw three sides — the shape is open because a side is missing. The count is the bug: B.
- The 90-degree angle is correct for a square (A would draw a triangle); the distance only changes size (C); squares have four sides (D false).
Answer: B — the loop repeats 3 times instead of 4.
When the condition points the wrong way
A condition bug passes casual testing because the program still does something sensible-looking — it just does it in the wrong cases. 'If lives = 0, add a bonus life' gives eternal life to the defeated and nothing to the struggling. To catch condition bugs, test the boundary: choose values just above, just below and exactly on the comparison point, and check each branch fires when it should.
Worked example
A quiz program should show 'Well done' when a player scores more than 7 out of 10. It shows 'Well done' for scores of 3 and 5, but not for 9. The code says: if score < 7 then show 'Well done'. What is the fix? A: Change the message text. B: Change the condition to score > 7. C: Change the quiz to 20 questions. D: Delete the if statement.
- The message appears for low scores and not for high ones — the comparison is backwards. Changing < to > (B) fixes exactly that.
- The text (A), the quiz length (C) and removing selection entirely (D) do not address the reversed condition.
Answer: B — the condition must be score > 7.
Testing like a scientist
Random trying is slow. Systematic testers change one thing at a time, keep notes, and choose test cases deliberately: a typical case, an edge case (the largest or smallest sensible input), and a case that broke things before. After a fix, re-run the old tests — programmers call this checking for regressions, because fixing one bug can quietly create another nearby.
Worked example
A program adds 10% to a price. It works for 100 (giving 110) but gives the wrong answer for 0. What is the best next step? A: Rewrite the whole program. B: Test the edge case 0 carefully and trace what the program does with it, since typical cases already work. C: Ignore it — nobody enters 0. D: Change all the numbers until it works.
- The typical case passes, so the fault is specific; trace the failing edge case — B.
- Rewriting (A) discards a mostly-working program; ignoring (C) leaves a known bug; random changes (D) are not testing.
Answer: B — trace the edge case to find where the logic breaks.
Watch out
- Changing several things at once — you learn nothing about which change mattered.
- Only testing the happy path — edge cases (0, maximum, exactly on the boundary) expose the most bugs.
- Assuming the computer made a mistake — it followed the instructions; the instructions are wrong.
- Fixing the symptom instead of the cause — find the first line where behaviour departs from intention.
Quick check
Try these on your own first — then reveal each answer.
1. What is a bug, and what is debugging?
2. A program rewards players when they fail and not when they succeed. Which bug family is it?
3. Why should you re-run old tests after fixing a bug?
Was this lesson helpful?
Practise this concept
Reading is the first step — the concept sticks when your child answers questions on it. Registered students practise graded questions for this lesson, with instant marking and worked explanations, and the lesson is marked complete in their progress.