Skip to content
SPM Tuition
Computer Science · Advanced programming practice

Debugging syntax, logic and runtime errors

Your program shows an error or a wrong answer and you do not know where to look.

The three kinds of error show up at different moments, and the moment tells you where to look. A syntax error stops the program before it starts, a runtime error stops it midway, and a logic error lets it finish with the wrong answer.

This lesson is part of advanced programming practice. For a wider view of program structure, see program structure and debugging.

How do I tell the three kinds apart?

Ask when the problem shows up and what the computer tells you.

Type When it shows What you see Who finds it
Syntax error Before the program runs An error message naming a line The language reports it
Runtime error While the program runs The program stops with a message The language reports it, for certain inputs
Logic error After the program finishes A wrong output, no message You, by tracing or testing

Logic errors are the hardest because nothing tells you they exist. That is why expected outputs matter, as the next lesson shows.

Worked example: one program, three errors

This original program should print the average of the first n marks in an array, for n = 3. The marks are 60, 70, 80.

marks = [60, 70, 80]
n = 3
total = 0
FOR i = 1 TO n
    total = total + marks[i]
NEXT i
PRINT "Average is " total / n

Suppose your language needs a comma or a plus sign between the text and the value in the output line. Leaving it out is a syntax error: the language stops and names the PRINT line. The fix is to write the line the way your language requires.

Now suppose the syntax is fixed and the array starts at 0. The loop runs i = 1, 2, 3. At i = 3, marks[3] does not exist, which is a runtime error. Fix the bound: FOR i = 0 TO n − 1.

Suppose instead the loop ran FOR i = 1 TO 2 to avoid the crash. It now runs, but total is 70 + 80 = 150, and 150 ÷ 3 = 50. The expected value is 70, so this is a logic error: it skipped marks[0].

The mistake: fixing the symptom, not the cause

A common slip is to silence the message without asking why it appeared. Shortening the loop to avoid the crash above removed the runtime error and created a logic error.

Approach Change Result
Symptom fix FOR i = 1 TO 2 No crash, average is 50, wrong
Cause fix FOR i = 0 TO n − 1 Total 210, average 70, correct

After every fix, rerun with the marks 60, 70, 80 and confirm that the answer is 70.

A routine for hunting errors

  1. Read the error message. If there is one, go to the line it names and the line above.
  2. If there is no message but the output is wrong, choose a small input and compute the expected output by hand.
  3. Trace the program with that input, and write each variable in a table.
  4. Find the first row where the table and your expectation differ.
  5. Make the smallest change that fixes that row, then rerun the whole trace.

Step 2 matters most. Without a hand-computed answer, you cannot tell that a program is wrong.

Check yourself

This code should print the sum of the even numbers from 1 to 10, which is 30. It prints 55. Name the kind of error and fix it.

sum = 0
FOR i = 1 TO 10
    sum = sum + i
NEXT i
PRINT sum
Answer

It is a logic error. The program is valid and runs, but it adds every number from 1 to 10, which is 55. There is no test for even numbers.

Fix: add a condition inside the loop.

IF i MOD 2 = 0 THEN
    sum = sum + i
ENDIF

Check: 2 + 4 + 6 + 8 + 10 = 30. The loop bounds were already right, so only the body changed, which is a minimal fix.

What to study next

Knowing the error type is half the job. Next, learn to show a program is correct with creating a test table with expected outputs. If your code runs but gives the wrong result, read my code runs but gives the wrong result.

If you want a teacher to hunt errors in your own program with you, see online one-to-one Computer Science tuition.

Common questions

What is the difference between a syntax error and a logic error?

A syntax error breaks the rules of the language, so the program will not run. A logic error follows the rules but does the wrong thing, so the program runs and gives a wrong result. The computer reports the syntax error, but not the logic error.

What is a runtime error?

An error that appears only while the program is running, such as dividing by zero or reading an array index that does not exist. The code is valid, but one input makes an operation impossible.

Why does my program run but give the wrong answer?

That points to a logic error. Trace the code with a small input, compare each variable with what you expected, and find the first line where they differ. The error is at or just before that line.

Should I rewrite the program if it has several errors?

Fix one error at a time and rerun, because one error can hide or cause another. A small correction that you can explain is better than a full rewrite that you cannot.

If you fix one error and another appears, one-to-one Computer Science lessons let a teacher go through your code with you and teach the order in which to hunt for each kind.

  • 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.