A syntax error breaks the rules of the language, so the program will not run. A logic error follows the rules, runs to the end, and gives the wrong answer.
This lesson belongs to program structure and debugging. It follows diagnosing a loop that terminates too early or never terminates.
What is the one test to apply?
Ask: did the program run to the end?
| Observation | Type of fault |
|---|---|
| Program refuses to run and reports a rule broken | Syntax error |
| Program stops part-way on an impossible operation such as dividing by zero | Runtime error |
| Program finishes but the answer is wrong | Logic error |
Only the last type needs you to test and trace. The first two announce themselves.
Worked example: a program with two faults
This program should output the average of three marks.
INPUT m1
INPUT m2
INPUT m3
IF m1 > 0
SET total = m1 + m2 + m3
END IF
OUTPUT total / 2
There are two faults. The first is the missing THEN after IF m1 > 0. Most languages would refuse to run the program, so this is a syntax error.
After adding THEN, the program runs. Enter 60, 70 and 80. The total is 210, and the output is 210 ÷ 2 = 105, but the correct average is 210 ÷ 3 = 70. That is a logic error: the divisor is wrong.
Why does the order of fixing matter?
You can only see the logic error after the syntax error is gone. The wrong average never appears while the program refuses to start.
| Stage | What you see | Action |
|---|---|---|
| Before any fix | Program will not run | Fix the missing THEN |
| After the syntax fix | 105 on screen | Test against a known answer |
| After the logic fix | 70 on screen | Re-test with a second input |
A common mistake is to stop when the program first runs. A program that runs is not a program that is correct.
Check yourself
Classify each fault as syntax, runtime or logic.
- (a)
WHLE x < 5typed instead ofWHILE. - (b) Area computed as length + width instead of length × width.
- (c) Dividing a total by a count that turns out to be zero.
- (d)
IF mark = 50where the rule is 50 or more.
Answer
(a) Syntax, a misspelt keyword. (b) Logic, the program runs with a wrong formula. (c) Runtime, since division by zero stops the program while it runs. (d) Logic, because 65 would fail a rule that should accept it, yet nothing stops.
What to study next
Next, learn how to write the fix in an answer in explaining a small correction instead of replacing the whole program. Keep track of your slips with the mistake log and paper-error review.
If you want a teacher to give you faulty programs to classify, see online one-to-one Computer Science tuition.