Identifying and Correcting Errors
1. Key Terms
| Term | Meaning |
|---|---|
| Bug | An error in a program that stops it working as intended |
| Debugging | The process of finding and removing errors from a program |
| Dry run | Working through an algorithm by hand, on paper, without running it on a computer |
| Trace table | A table used to record the value of every variable, output and prompt at each step of a dry run |
2. Types of Error
Syntax errors
A syntax error breaks the rules of the language: a misspelled keyword, a missing bracket, a missing quotation mark. The program will not translate, so it cannot run at all.
Logic errors
A logic error means the program runs perfectly but produces the wrong result. The syntax is legal, so nothing warns you. These are the errors trace tables are designed to catch.
| Syntax error | Logic error | |
|---|---|---|
| Does the program run? | No | Yes |
| How it is spotted | The translator reports it | The output is wrong — found by testing or a dry run |
| Example | OUPUT Total | Using + where * was intended |
3. The Five Errors That Appear Most Often
Exam questions give you a short algorithm containing deliberate mistakes. Almost all of them are one of the following.
(a) Initialising inside the loop
Symptom: the total equals the last value entered.
(b) Off-by-one loop bounds
Symptom: one item is always missed, or the program tries to read past the end of an array. Check the count carefully: 1 TO 10 runs ten times, 0 TO 10 runs eleven.
(c) The wrong comparison operator
Symptom: everything works except at the exact boundary. Confusing > with >= is the single most common logic error in this topic.
(d) An infinite loop
Cause: the variable in the loop condition is never updated inside the loop. Every condition-controlled loop must contain something that eventually makes the condition false.
(e) A swap without a temporary variable
4. How to Find an Error Systematically
Do not read an algorithm hoping the mistake will stand out. Work through it:
- Read the stated purpose. You cannot judge whether an algorithm is wrong without knowing what it is meant to do.
- Check every initialisation. Are totals and counters set before the loop, not inside it?
- Check every loop bound. Count the iterations by hand.
- Check every comparison. Should it be > or >=?
- Dry run with a trace table using a small amount of data — three or four values is enough to expose most errors.
- Test the boundary specifically, because that is where operator errors hide.
Worked example
This algorithm should output the highest of five numbers. Find the errors.
| Line | Error | Correction |
|---|---|---|
| Highest ← 0 | Fails if all five numbers are negative | Input the first number and set Highest to it |
| FOR Count ← 1 TO 4 | Only four numbers are read, not five | FOR Count ← 1 TO 5 |
| IF Number < Highest | Wrong operator — this finds the lowest | IF Number > Highest |
5. Exam Focus
Quick self-check
- State the difference between a syntax error and a logic error, and how each is detected.
- Why does a total initialised inside a loop give the last value entered?
- Write the three lines that correctly swap two values.
- Give one reason a WHILE loop might never end.
- An algorithm should accept marks of 40 and above. It uses IF Mark > 40. Which test data exposes the error?