Short on time? This page is the recap. For explanations and worked examples, use the full study guide.
Recap
- Syntax error: the language’s rules are not followed.
- Logic error: the algorithm or program behaves incorrectly or unexpectedly; it still runs.
- Run-time error: a mistake that occurs during execution; each language defines its own.
- Overflow error: a value outside the representable range (integers use a fixed number of bits in many languages).
- Testing uses defined inputs to check expected outputs, which come from the requirements.
- Good tests show the different expected outcomes and include values at or just beyond the extremes and at boundaries.
- Ways to find errors: test cases, hand tracing, visualisations, debuggers, extra output statements.
- After a correction, retest.
Key relationships
| Symptom | Likely error |
|---|---|
| Will not run; points to a line | Syntax |
| Runs, shows a wrong answer | Logic |
| Stops during running (for example, divide by zero) | Run-time |
| Number too big for its storage | Overflow |
| Condition wrong by exactly one value | Logic error at a boundary (> or >=) |
| Loop does one pass too many or too few | Logic error (off by one) |
Assumptions and conventions
- In a trace table, record each variable after every statement or after each pass, and keep the columns consistent.
- Compare expected with actual output; the expected value comes from the requirement.
- Pseudocode here is Marlbridge’s rendering of the reference notation:
←assigns,*multiplies,>=means at least.
Mistakes to avoid
- Calling every error a “syntax error”.
- Taking expected outputs from the program under test.
- Using only typical values and skipping boundaries.
- Making the trace table too coarse, so the error is stepped over.
- Stopping after the first failing test without retesting the whole set after a fix.
- Forgetting that a fix can break something else.
Two quick worked examples
1. Requirement: “free entry for ages under 12”. Code: IF (age <= 12). The input 12 should be refused free entry but is accepted, so this is a logic error at the boundary; the test inputs 11, 12 and 13 reveal it.
2. A list has 4 items and the code asks for item 5. The program stops while running: a run-time error. Adding DISPLAY(LENGTH(list)) just before the line would show the length is 4, which is an example of using an extra output statement to find an error.
A revision routine
Cover the page and write out the four error types with one example each. Then take any short code segment from the study guide, write a requirement for it, choose five test inputs including two boundary values, and predict the expected outputs before tracing the code. Finally trace the code in a table and compare. Practising in this order, requirement first and code second, stops you from accepting whatever the program happens to print.
Quick self-check
- The requirement is “pass if score is at least 40”. The code tests
score > 40. Which input shows the error? (40: expected pass, actual fail.) - A loop meant to add 1 to 10 runs 9 times. Error type? (Logic error: off by one.)
- A program stops when it divides a total by a count of zero. Error type? (Run-time error.)
Next: practice questions.