Identifying and Correcting Errors
The four kinds of program errors, how to track them down, and how to choose test inputs that catch them before users do.
1Four kinds of errors
An error (often called a bug) is anything that keeps a program from working as intended. Errors come in four kinds, and each shows up at a different moment.
Syntax error: the rules of the programming language are not followed, so the program cannot run at all. This segment never closes its {:
score ← 95
IF (score > 90)
{
DISPLAY("A")
Nothing is displayed; the program is rejected before it starts. Fix it by adding the missing }.
Run-time error: the program starts, but a statement fails during execution. Each programming language defines its own run-time errors; dividing by zero and using a list index that does not exist are common ones:
scores ← [88, 92, 75] DISPLAY(scores[4])
The list has 3 items, so scores[4] does not exist and the program stops at that line. Using scores[3] displays 75.
Logic error: a mistake in the algorithm or program that makes it behave incorrectly or unexpectedly. It runs, it finishes, and the answer is wrong:
width ← 3 height ← 4 area ← width + height DISPLAY(area)
It displays 7, but a 3 by 4 rectangle has area 12. The grammar is fine; the reasoning is wrong. Changing + to * fixes it. The only way to notice a logic error is to know what the right answer should be.
Overflow error: the computer tries to handle a number outside the range of values it can store. A counter stored in 8 bits can hold 0 through 255. Adding 200 + 200 gives 400, which does not fit. Some systems stop with an error; others keep only what fits and produce a wrong number (here 144). The formula was right; the space was too small.
2Finding errors
Effective ways to find and correct errors:
| Method | What you do |
|---|---|
| Test cases | Run the program on chosen inputs and compare the results with what should happen. |
| Hand tracing | Follow the code on paper, one statement at a time, writing down every variable’s value. |
| Visualizations | Draw or display the data (a chart, a picture of a list) so a wrong value stands out. |
| Debuggers | Use a tool that pauses the program at a chosen line and shows the variables’ values. |
| Extra output statements | Add DISPLAY statements to show values partway through, then remove them. |
Whichever method you use, the goal is the same: find the first place where what the program does differs from what you expected, because the error is at or before that point.
3Testing with chosen inputs
Testing runs a program on defined inputs (inputs chosen in advance) to check that it produces the expected outcomes. Programmers use the results to revise the program. The expected outcome must come from the program requirements, never from running the program; without the requirements you can’t choose good inputs or tell a right answer from a wrong one.
Good test inputs show each different expected outcome at and just beyond the extremes, meaning the minimum and maximum of the allowed input. Bugs gather at the edges.
Requirement: a summer program accepts ages 13 through 18. The programmer wrote this check:
IF (age > 13 AND age ≤ 18)
{
DISPLAY("eligible")
}
ELSE
{
DISPLAY("not eligible")
}
| Input age | Why test it | Expected | Actual | Result |
|---|---|---|---|---|
| 15 | typical | eligible | eligible | pass |
| 12 | just below the minimum | not eligible | not eligible | pass |
| 13 | the minimum | eligible | not eligible | fail |
| 18 | the maximum | eligible | eligible | pass |
| 19 | just above the maximum | not eligible | not eligible | pass |
The typical input 15 and even the maximum 18 pass. Only the minimum, 13, reveals the bug: age > 13 should be age ≥ 13. After that change all five tests pass.
Syntax errors break the language’s rules; run-time errors happen during execution; logic errors give wrong results; overflow errors involve numbers outside the storable range. Find errors with test cases, hand tracing, visualizations, debuggers, and extra output. Test with inputs at and just beyond the minimum and maximum, with expected outcomes taken from the requirements.
4Worked examples
Four bugs
Name each error. (a) A program won’t start because a keyword is misspelled. (b) A grade average displays 79 instead of 85 because it divides by the wrong count. (c) A program stops when a user enters 0 people and it divides by that number. (d) A game’s score, stored in 16 bits, turns negative after a very high score.
- 1(a) The language’s rules are broken before anything runs: syntax.
- 2(b) It runs and finishes with a wrong answer: logic.
- 3(c) It stops during execution on an impossible statement: run-time.
- 4(d) The number grew past the range 16 bits can hold: overflow.
The largest number
This segment should display the largest value in nums. It works for [4, 11, 7]. Find an input that exposes the bug, then fix it.
max ← 0
FOR EACH n IN nums
{
IF (n > max)
{
max ← n
}
}
DISPLAY(max)
- 1Try an extreme: all negative values, [-4, -9, -2]. Expected: -2.
- 2Actual:
0. No value is greater than 0, somaxnever changes, and 0 is not even in the list. - 3Logic error: the starting value is not one of the list’s values. Start with the first item instead:
max ← nums[1] FOR EACH n IN nums { IF (n > max) { max ← n } } DISPLAY(max)
-2 and [4, 11, 7] still gives 11.The discount
Requirement: orders from $50 through $100 get a 10% discount; other orders get none. Choose test inputs and expected outcomes.
- 1Typical: $75, discount.
- 2At the extremes: $50 and $100, both discount.
- 3Just beyond: $49 and $101, no discount.
The loop that never ends
With n = 5 this segment should display 1 + 3 + 5 = 9, but it never stops. A DISPLAY(count) was added inside the loop to investigate.
count ← 1
sum ← 0
REPEAT UNTIL (count > n)
{
count ← 1
sum ← sum + count
count ← count + 2
DISPLAY(count)
}
DISPLAY(sum)
- 1The extra output shows
countafter each pass: 3, 3, 3, 3, … It never grows. - 2Line 5 resets
countto 1 on every pass, socountis always 3 at the check andcount > nis never true. - 3Remove line 5 (and the extra
DISPLAY):count ← 1 sum ← 0 REPEAT UNTIL (count > n) { sum ← sum + count count ← count + 2 } DISPLAY(sum)
n = 5 displays 9. It was a logic error: valid code, wrong algorithm.5Common mistakes
Values in the middle of the range rarely find bugs. Test the minimum, the maximum, and just beyond each.
If you write down whatever the program printed as “expected,” the test can never fail. Expected outcomes come from the requirements.
A program that stops partway on an impossible statement has a run-time error. A logic error runs to the end with a wrong result.
With overflow the formula is right and the number is too big (or too small) for the space it is stored in.
“With the input 13, the program displays ‘not eligible’, but the requirement says 13 is eligible. The condition age > 13 leaves out 13. This is a logic error; changing it to age ≥ 13 makes all five tests pass.”
Give the input, the expected and actual outcomes, the cause, the type of error, and the fix.