---
title: "Identifying and Correcting Errors | FiveStudy"
description: "Syntax, run-time, logic, and overflow errors, the ways to find and fix them, and how to choose test inputs at and just beyond the extremes."
url: https://fivestudy.com/guides/ap-csp-1-4-identifying-and-correcting-errors
---

[AP Computer Science Principles](https://fivestudy.com/ap-computer-science-principles) · Study guide

# Identifying and Correcting Errors

[Figure: FiveStudy]

FiveStudy

AP® Computer Science Principles · Big Idea 1 · Creative Development

AP® Computer Science Principles · Big Idea 1 · Creative Development

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

1. Four kinds of errors 2. Finding errors 3. Testing with chosen inputs 4. Worked examples 5. Common mistakes

## 1 Four 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.

**Figure 1.** The moment an error shows up tells you its type.

## 2 Finding 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.

## 3 Testing 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 |

**Figure 2.** Five test inputs: one typical, the two extremes, and one just beyond each. The bug in the age check shows up only at 13.

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.

The key idea

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.

## 4 Worked examples

Worked Example 1 Classify the error

### 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. 1

   (a) The language’s rules are broken before anything runs: syntax.

2. 2

   (b) It runs and finishes with a wrong answer: logic.

3. 3

   (c) It stops during execution on an impossible statement: run-time.

4. 4

   (d) The number grew past the range 16 bits can hold: overflow.

**Answer:** (a) syntax, (b) logic, (c) run-time, (d) overflow.

Worked Example 2 Find and fix

### 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)
```

1. 1

   Try an extreme: all negative values, [-4, -9, -2]. Expected: -2.

2. 2

   Actual: `0`. No value is greater than 0, so `max` never changes, and 0 is not even in the list.

3. 3

   Logic 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)
   ```

**Answer:** With the fix, [-4, -9, -2] gives `-2` and [4, 11, 7] still gives `11`.

Worked Example 3 Choose test inputs

### The discount

Requirement: orders from $50 through $100 get a 10% discount; other orders get none. Choose test inputs and expected outcomes.

1. 1

   Typical: $75, discount.

2. 2

   At the extremes: $50 and $100, both discount.

3. 3

   Just beyond: $49 and $101, no discount.

**Answer:** $49 none, $50 discount, $75 discount, $100 discount, $101 none. Each expected outcome comes from the requirement.

Worked Example 4 Debug with output

### 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)
```

1. 1

   The extra output shows `count` after each pass: 3, 3, 3, 3, … It never grows.

2. 2

   Line 5 resets `count` to 1 on every pass, so `count` is always 3 at the check and `count > n` is never true.

3. 3

   Remove line 5 (and the extra `DISPLAY`):

   ```
   count ← 1
   sum ← 0
   REPEAT UNTIL (count > n)
   {
     sum ← sum + count
     count ← count + 2
   }
   DISPLAY(sum)
   ```

**Answer:** Now `n` = 5 displays `9`. It was a logic error: valid code, wrong algorithm.

## 5 Common mistakes

Common Mistake 1 · Testing only typical inputs

Values in the middle of the range rarely find bugs. Test the minimum, the maximum, and just beyond each.

Common Mistake 2 · Taking the expected outcome from the program

If you write down whatever the program printed as “expected,” the test can never fail. Expected outcomes come from the requirements.

Common Mistake 3 · Calling every crash a logic error

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.

Common Mistake 4 · Calling overflow a logic error

With overflow the formula is right and the number is too big (or too small) for the space it is stored in.

Report an error and its fix like this

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

FiveStudy AP® Computer Science Principles · Identifying and Correcting Errors
