CIE AS & A Level Computer Science, section 12.3 (Program testing and maintenance)
Types of error
| Error | Cause | When found | Example |
|---|---|---|---|
| Syntax error | breaks the rules of the language | when the program is translated (or as you type in an IDE) | a missing bracket, or IF without THEN |
| Logic error | the program runs but gives the wrong result because the algorithm is wrong | by testing with known results | using + instead of *, or a loop that ends one time too early |
| Run-time error | the program crashes while running, because of an operation that cannot be carried out | while running | division by zero, reading past the end of an array, a missing file |
Finding errors
- Dry run with a trace table: follow the program by hand to see where values go wrong.
- Use the IDE tools: single stepping, breakpoints and variable watches.
- Add output statements to display variable values.
- Read the error message, which usually gives a line number for syntax and run-time errors.
Examples in Python
# syntax error: missing colon
# if x > 3
# print(x)
# logic error: should be total / count, not total * count
total, count = 90, 3
average = total * count
print(average) # 270, wrong
# run-time error
# print(10 / 0) # ZeroDivisionError
Correcting an error
- Locate the line where it happens.
- Identify which type it is and why.
- Change the code to fix it.
- Re-test to check the fix works and has not caused new problems.
Common mistake: thinking a logic error "stops the program". A logic error does not cause an error message: the program runs and gives a wrong answer, so it is the hardest to find. Also, a syntax error is found before the program runs.
Exam tip: when asked to identify an error, say which type it is and explain why. When asked to correct it, give the corrected line of code.
Check yourself
Which type of error produces no error message but gives incorrect results?
A logic error.
Give an example of a run-time error.
Division by zero, or reading an array element outside its bounds.
What is a dry run?
Working through the program by hand, with test data, using a trace table to find mistakes.