AP Computer Science Principles · Unit 1 lecture · Part 8 of 11 · 8 min

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

Mr. Thompkins
Every programmer writes bugs. The difference between a beginner and an expert isn't fewer bugs, it's knowing exactly what kind of bug you're looking at. The AP exam names four kinds. Let's meet them.

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.)

ErrorDoes it run?ExampleHow you notice
SyntaxNoDISPLAY(total with no )The language refuses to run it
LogicYes, to the endtotal > 50 when 50 should countWrong output for some inputs
Run-timeStarts, then stopstemps[4] in a 3-item listIt halts partway with an error
OverflowDepends on the language255 + 1 kept in 8 bitsA number too big for its storage
The four error types. The quickest test: did it run, did it finish, and was the answer right?
?Wait, what's a list index again?
A list stores values in order, and each position has a number called its index. On the AP exam the first element is at index 1, so temps[1] is the first temperature, and a list of 3 elements has valid indexes 1, 2 and 3 only.

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}
  1. line 3 Four passes, but the list has only three items
  2. line 5 On pass 4, temps[4] does not exist
output › 61 64 70, then an error
REPEAT 4 TIMES runs the block exactly four times.
passitemps[i]displayed
116161
226464
337070
44nonerun-time error
The trace: one row per pass. Pass 4 asks for temps[4], and the program stops.
?Wait, what does REPEAT n TIMES do?
It runs the code inside its braces exactly n times, then moves on. Here n is 4, so the two lines inside run four times, and i goes 1, 2, 3, 4.
Pause and think
Is a misspelled word inside a quoted message, like DISPLAY("Helo"), a syntax error?
I thought about it. Show me
No. The program follows every rule and runs, it just shows Helo. Text inside quotes can be anything. Whether that counts as a logic error depends on what the program was supposed to do, but it's definitely not a syntax error.
Name the error
Classify each problem as a syntax, logic, run-time or overflow error. (a) A grade program prints "B" for a score of 95. (b) A program won't start, and the editor points at a line that reads DISPLAY(total with no closing parenthesis. (c) A program that averages a list stops with an error when the list is empty. (d) A game stores each player's score in 16 bits, and a player who passes 65,535 points suddenly has a tiny score.
  1. (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.
  2. (b) Syntax error. The rules of the language are broken, so the program can't run at all.
  3. (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.
  4. (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
  1. First question for each: did the program run at all?
  2. If it ran, did it stop partway or finish?
  3. If it finished, is the problem a wrong answer, or a number too big to store?
Check yourself
A program runs to the end without any error message, but the total it displays is 10 more than the correct total. What type of error does it contain?
  1. ASyntax error
  2. BRun-time error
  3. CLogic error
  4. 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.

Check yourself
What happens when this code segment runs?
scores ← [88, 92]
DISPLAY(scores[1])
DISPLAY(scores[2])
DISPLAY(scores[3])
  1. AIt displays 88 92 and then a run-time error occurs.
  2. BIt displays 92 and then a run-time error occurs.
  3. CIt does not run, because of a syntax error.
  4. 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.