Debugging is the skill of finding why a program behaves differently from what you intended, and fixing it with the smallest change that works. It depends on the tracing habits from programming foundations.
This section gives four lessons that take you from locating a fault to explaining it clearly.
What does this section cover?
- Tracing variable scope across a procedure and its caller shows which variables exist where.
- Diagnosing a loop that terminates too early or never terminates deals with loop conditions.
- Distinguishing a syntax error from a logically wrong result sorts faults by type.
- Explaining a small correction instead of replacing the whole program teaches how to write the fix.
How do the lessons connect?
First you learn where values live, because a variable in the wrong place is a fault you cannot see. Then you study the most common structural fault, a loop that stops at the wrong time.
Next you learn to name the kind of fault, and last you practise explaining the smallest change that repairs it. In an answer, explaining the fix matters as much as making it.
A short bug to orient you
This program should output the average of three marks.
FOR i = 1 TO 3
SET total = 0
INPUT mark
SET total = total + mark
NEXT i
OUTPUT total / 3
Enter 60, 70 and 80. The correct average is 210 ÷ 3 = 70. The program outputs 80 ÷ 3, which is about 26.7.
A trace shows why: SET total = 0 sits inside the loop, so total resets on every pass and only the last mark survives. The fix is to move that line above the loop.
Who should start where?
| If you notice | Start with |
|---|---|
| A variable has a value you did not expect after a call | Scope lesson |
| The program freezes or stops too soon | Loop termination lesson |
| You cannot tell whether an error is a typo or a design fault | Syntax versus logic lesson |
| You rewrite a whole program to fix one line | Small correction lesson |
Test yourself with the section practice questions. The restricted pseudocode trace trainer lets you rehearse tracing on your own device, and the mistake log and paper-error review keeps track of your repeat slips.
If you would like a teacher to go through your own buggy programs, see online one-to-one Computer Science tuition.