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
- Read the error message. If there is one, go to the line it names and the line above.
- If there is no message but the output is wrong, choose a small input and compute the expected output by hand.
- Trace the program with that input, and write each variable in a table.
- Find the first row where the table and your expectation differ.
- 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.