Skip to content
SPM Tuition
Computer Science · Program structure and debugging

Syntax errors versus logic errors

You call every bug a syntax error and lose marks when the real cause is the logic.

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 < 5 typed instead of WHILE.
  • (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 = 50 where 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.

Common questions

How can I tell a syntax error from a logic error?

Ask whether the program runs. If it stops with a message because a rule of the language is broken, it is a syntax error. If it runs and gives a wrong result, it is a logic error.

Is a runtime error the same as a logic error?

No. A runtime error happens while the program is running, such as dividing by zero, and it stops the program. A logic error does not stop the program but gives a wrong result.

Which type of error is harder to find?

Logic errors, because no message points to them. You find them by testing with inputs whose correct answers you already know and by tracing the variables.

Can one program contain both types at once?

Yes. You must fix the syntax error first so that the program can run, then test the output to find any logic error that remains.

If you keep naming the wrong kind of error in an answer, a one-to-one Computer Science teacher can give you fresh faulty programs and mark how you classify them.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.