1.4 Identifying and correcting errorsFour kinds of error, and how to tell them apart

A syntax error is a mistake in which the rules of the programming language are not followed: a missing parenthesis, a misspelled command, a brace that never closes. Because the computer can't understand the code at all, a program with a syntax error won't run. In most languages, you find out right away.
A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. The code follows every rule, it runs all the way to the end, and it gives the wrong answer, at least for some inputs. Logic errors are the sneaky ones, because nothing warns you. Using > where you needed ≥ is the classic.
A run-time error is a mistake in the program that occurs during its execution. The program starts fine and then fails partway through. Each programming language defines its own run-time errors, but two show up on the AP exam again and again: dividing by zero, and using a list index that doesn't exist.
An overflow error happens when a computer attempts to handle a number that is outside the defined range of values it can store. Many languages store whole numbers in a fixed number of bits, so there is a largest number they can hold. You'll see exactly why in Unit 2. (The exam's own pseudocode has no size limit on integers, so overflow questions describe a fixed number of bits.)
| Error | Does it run? | Example | How you notice |
|---|---|---|---|
| Syntax | No | DISPLAY(total with no ) | The language refuses to run it |
| Logic | Yes, to the end | total > 50 when 50 should count | Wrong output for some inputs |
| Run-time | Starts, then stops | temps[4] in a 3-item list | It halts partway with an error |
| Overflow | Depends on the language | 255 + 1 kept in 8 bits | A number too big for its storage |
?Wait, what's a list index again?
Here is a run-time error in action. The list has three temperatures, but the loop runs four times. The first three passes work perfectly, then the program asks for an element that isn't there.
1temps ← [61, 64, 70]2i ← 13REPEAT 4 TIMES14{5 DISPLAY(temps[i])26 i ← i + 17}
- line 3 Four passes, but the list has only three items
- line 5 On pass 4, temps[4] does not exist
| pass | i | temps[i] | displayed |
|---|---|---|---|
| 1 | 1 | 61 | 61 |
| 2 | 2 | 64 | 64 |
| 3 | 3 | 70 | 70 |
| 4 | 4 | none | run-time error |
?Wait, what does REPEAT n TIMES do?
I thought about it. Show me
- (a) Logic error. It runs and finishes, but the result is wrong.
Why?
The language rules were followed, so it's not syntax. Nothing crashed, so it's not run-time. - (b) Syntax error. The rules of the language are broken, so the program can't run at all.
- (c) Run-time error. It starts, then fails during execution: the average divides by LENGTH(list), which is 0, and dividing by zero is a run-time error.
- (d) Overflow error. 16 bits hold whole numbers up to 65,535, and a larger score is outside that range.
Why?
2 to the 16th power is 65,536 patterns, so 0 to 65,535. Unit 2 explains why it wraps to a small number.
Answer: (a) Logic. (b) Syntax. (c) Run-time (division by zero). (d) Overflow.
Stuck? Get a hint
- First question for each: did the program run at all?
- If it ran, did it stop partway or finish?
- If it finished, is the problem a wrong answer, or a number too big to store?
- ASyntax error
- BRun-time error
- CLogic error
- DOverflow error
Show the answer
C. It ran and finished, so the code follows the language rules and didn't fail during execution. It just computes the wrong value, which is a logic error. Run-time is the trap: run-time errors stop the program partway.
scores ← [88, 92] DISPLAY(scores[1]) DISPLAY(scores[2]) DISPLAY(scores[3])
- AIt displays 88 92 and then a run-time error occurs.
- BIt displays 92 and then a run-time error occurs.
- CIt does not run, because of a syntax error.
- DIt displays 88 92 0.
Show the answer
A. Indexes start at 1, so scores[1] is 88 and scores[2] is 92. scores[3] doesn't exist, and the program fails while running: a run-time error. The code follows the rules, so it's not a syntax error. The second choice counts from 0.
- Syntax error: the language's rules are broken; the program won't run.
- Logic error: it runs and finishes but behaves incorrectly or unexpectedly.
- Run-time error: it fails during execution, such as dividing by zero or an index outside the list.
- Overflow error: a number is outside the range the computer can store in its fixed number of bits.