← Back to course
2/6
Week 08 · Test, Debug, Document

Tuesday

Four kinds of errors
// Test plans, four kinds of errors, and guides that help others fix problems
⏱ about 20 min

Tuesday: Four Kinds of Errors

Comet lays four pages of Beacon pseudocode on the bench. "Each one has a bug," she admits. "I wrote them all."

Wren grins. "Then you have given us four clues."

Nova sweeps a beam of light over the pages, and a small glowing marker appears beside each one.

"Let's debug together," Nova says. "But first, name each kind of bug. Different kinds hide in different places."

Comet picks up the first page. "This one will not even run. I spelled a word wrong."

"That is one kind," Wren says. "Let's find the other three."

Four kinds of errors

A logic error is a mistake in the algorithm or program that makes it behave incorrectly or unexpectedly.

A syntax error is a mistake where the rules of the programming language are not followed.

A run-time error is a mistake that occurs while the program is running.

An overflow error happens when a computer tries to handle a number outside its defined range of values.

Comet's four pages

PAGE 1
REPEAT 24 TIMS
{
   checkPlant(waterLevel)
}

PAGE 2 (requirement: alert when the level is below 30)
IF (waterLevel ≤ 30)
{
   DISPLAY("The tomatoes need water")
}

PAGE 3
readings ← [40, 55, 62]
DISPLAY(readings[4])

PAGE 4 (readingCount can hold whole numbers from 0 to 255)
readingCount ← 255
readingCount ← readingCount + 1
NAME EACH BUG
  • Read the question.
  • Tap your answer.
Page 1 spells TIMES as TIMS. What kind of error is that?
Page 2 runs, but it alerts at exactly 30 when it should not. What kind of error is that?
Page 3 asks for index 4 of a list with 3 elements. What kind of error is that?
Page 4 tries to make readingCount 256, but it can only hold 0 to 255. What kind of error is that?

On the AP reference sheet, list indexes run from 1 to the length of the list.

If an index is less than 1 or greater than the length, an error message is produced and the program stops running.

A run-time error in a loop

Here is a loop that displays every reading. Trace it, and watch the index.

readings ← [40, 55, 62]
index ← 1
REPEAT UNTIL (index > 4)
{
   DISPLAY(readings[index])
   index ← index + 1
}
TRACE THE LOOP
  • Read the question.
  • Tap your answer.
What does the loop display on its first three passes?
On the fourth pass, index is 4. What happens?
Which change fixes the loop for a list of any length?
StatementTrue or false?
A program with a logic error can run without any error message.?
A syntax error breaks the rules of the programming language.?
Asking for index 0 of a list is fine on the AP reference sheet.?
Comet should be embarrassed about her bugs.?
WHY THIS EXERCISEEvery bug is a clue. Naming its kind tells you where to look.
A mistake where the language rules are not followed is a ____ error.
A program that runs but does the wrong thing has a ____ error.
On paper, make a four-box chart, one box for each kind of error. In each box, write its name and one example from Beacon.

Nice bug hunting. Tomorrow's Beacon Lab tracks down a quiet logic error.

← Monday