AQA GCSE Computer Science (8525), section 3.2.11 (Robust and secure programming)
Types of error
| Error | Meaning | Detected |
|---|---|---|
| Syntax error | the code breaks the rules of the programming language | when the program is translated or run: it will not run |
| Logic error | the program runs, but gives the wrong result because the algorithm is wrong | by testing; there is no error message |
Examples
Syntax errors
# print("Hello" missing bracket
# if x > 5 missing colon
# for i in range(5) missing colon
Logic errors
# Average of two numbers, but the brackets are missing
a = 10
b = 20
average = a + b / 2
print(average) # 20.0, but the average should be 15
Fixed:
average = (a + b) / 2 # 15.0
Other logic errors: using < instead of <=, a loop that runs one time too many or too few, or using the wrong variable.
Finding errors
- Read the error message: it often gives the line number of a syntax error.
- Use a trace table to follow the algorithm by hand.
- Test with data where you know the answer.
- Add extra output statements to see the values of variables.
Correcting errors
- Find the line that is wrong.
- Decide what type of error it is.
- Change the code.
- Test again.
Categorising an error
When asked to identify an error, quote the faulty line, say whether it is a syntax or logic error, and explain why.
Example: IF mark > 50 THEN when "50 or more" is required is a logic error, because 50 would be wrongly rejected. It should be mark ≥ 50.
Common mistake: saying a logic error "stops the program". It does not: the program runs but gives the wrong answer. A syntax error is the one that stops the program from running.
Exam tip: name the type of error and explain why: syntax errors break language rules, logic errors give wrong results. Then give the corrected line.
Check yourself
What is the main difference between a syntax error and a logic error?
A syntax error breaks the rules of the language so the program will not run, whereas a logic error lets it run but gives wrong results.
A program calculates (a + b / 2) for the average. What type of error is this?
A logic error, because brackets are missing so the wrong calculation is done.
How can a trace table help to find errors?
It shows the value of each variable at every step, so you can see where the result goes wrong.