impera Open Impera

AP Computer Science Principles · Creative development

Identifying and correcting errors

1Types of error

  • Syntax error: the code breaks the rules of the language, e.g. a missing bracket or misspelled keyword. The program will not run.
  • Run-time error: the program starts but fails during execution, e.g. dividing by zero or accessing a list index that does not exist.
  • Logic error: the program runs but gives wrong results because the algorithm is incorrect, e.g. using < instead of ≤.
  • Overflow error: a number is too large for the space available to store it (see "Binary numbers").

Logic errors are often the hardest to find, because nothing crashes.

Check yourself

A program to average three scores divides the total by 2. This is a:

  • Logic error

  • Run-time error

  • Overflow error

  • Syntax error

Show the answer

Logic error

It runs but gives the wrong answer.

2Finding errors

Strategies:

  • Test cases: choose inputs and their expected outputs, then compare with actual outputs.
  • Hand tracing: step through code on paper, recording variable values.
  • Visualisations and debugging tools that show values as the program runs.
  • Adding output statements (e.g. DISPLAY) to show intermediate values.
Worked example

a procedure should return the largest value in a list but returns 0 for [−5, −2, −9].

max ← 0
FOR EACH n IN nums
{
   IF (n > max)
   {
      max ← n
   }
}
RETURN (max)
  1. Trace: max starts at 0; no negative value exceeds 0, so 0 is returned.
  2. Fix: initialise max ← nums[1] (the first element; AP lists start at index 1).

A trace table reveals the logic error: max starts at 0 and never changes

Check yourself

Which technique records the values of variables step by step on paper?

  • Encryption

  • Compiling

  • Compression

  • Hand tracing

Show the answer

Hand tracing

Trace table.

3Choosing test cases

Good testing uses several kinds of input:

  • Normal (typical) values.
  • Boundary values at the edges of valid ranges (e.g. 0, 100, exactly the cut-off).
  • Invalid or unexpected values (empty lists, negative numbers, text instead of numbers).
Worked example

a procedure returns "pass" when score ≥ 60. Test 75 (normal), 60 and 59 (boundaries), −5 and 101 (invalid).

Check yourself

For a rule "age must be 13 or older", which pair of test values checks the boundary?

  • 13 and 50

  • 1 and 100

  • 12 and 13

  • 20 and 30

Show the answer

12 and 13

Either side of the cut-off.

4Fixing errors

After finding an error:

  1. Identify the exact line or condition responsible.
  2. Change it.
  3. Re-test, including earlier test cases, to check the fix works and has not broken anything else.

Programs that do not cover all cases may still contain errors even when all test cases pass. Testing can show the presence of errors, not prove their absence.

Check yourself

After fixing a bug, a programmer should:

  • Delete all test cases

  • Only test the changed line

  • Release immediately without testing

  • Re-run earlier tests as well as new ones

Show the answer

Re-run earlier tests as well as new ones

Avoid new bugs.

5Going deeper: the four kinds of error, test cases and debugging methods

Four kinds of error

Syntax error: the rules of the programming language are broken: a missing bracket, a misspelled keyword, an unclosed quotation mark. The program will not run at all; the language tool usually points to the line.

Run-time error: the program starts but fails during execution: dividing by zero, using a list index that does not exist, using a variable with no value. What counts as a run-time error is defined by the language.

Logic error: the program runs to the end but behaves incorrectly or gives the wrong result. The computer does exactly what was written, which is not what was meant. These are the hardest to find, since nothing announces them.

Overflow error: a number is too large for the range the computer can represent. With a fixed number of bits, a result past the maximum cannot be stored correctly.

Worked example (classify each)

  1. DISPLAY("Total" + ): syntax error.
  2. average ← total / count when count is 0: run-time error.
  3. A program meant to average three marks computes a + b + c / 3: logic error (only c is divided).
  4. A 4-bit counter at 15 is increased by 1: overflow.

Finding errors: testing

A test case is an input together with the expected output. Compare expected with actual.

Choose test cases that cover:

  • Typical values: ordinary input.
  • Boundary (extreme) values: at and just either side of the limits. Most errors live here.
  • Invalid or unexpected input: wrong type, empty input, negative numbers where only positives make sense.
  • Every branch of every conditional.

Worked example (designing tests): a procedure should return "pass" for marks of 50 or more and "fail" otherwise, for marks from 0 to 100.

  1. Typical: 75 → pass; 30 → fail.
  2. Boundaries: 49 → fail; 50 → pass; 51 → pass; 0 → fail; 100 → pass.
  3. Invalid: −5; 101; text.
  4. If the code says mark > 50, only the test at 50 exposes the error.

Finding errors: debugging techniques

  • Hand tracing: step through the code on paper with a table of variable values, one row per change.
  • Extra output statements: temporarily display variables at key points to see where values go wrong.
  • Visualizations and debuggers: tools that run a program one step at a time and show the values.
  • Test cases: narrow down which inputs fail.
  • Isolating: test one procedure at a time; comment out sections; halve the search area.
  • Explaining the code aloud to someone (or something) else.

Worked example (trace to find a logic error): intended to add the numbers 1 to 5.

total ← 0
count ← 1
REPEAT UNTIL (count = 5)
{
    total ← total + count
    count ← count + 1
}
DISPLAY(total)
  1. count 1: total 1. count 2: total 3. count 3: total 6. count 4: total 10.
  2. count becomes 5; the loop stops before adding 5.
  3. Displays 10, not 15.
  4. Fix: REPEAT UNTIL (count > 5).

This is an off-by-one error, the commonest kind of logic error in loops.

Worked example (another logic error): a program should display the larger of two numbers.

IF (first > second)
{
    DISPLAY(second)
}
ELSE
{
    DISPLAY(first)
}

It runs without complaint and always displays the smaller. Testing with 3 and 8 (expected 8, actual 3) reveals it.

Correcting errors: change one thing at a time, then re-run all the tests: a fix can break something else.

Why testing cannot prove a program correct: tests show the presence of errors for the inputs tried; they cannot show that no error exists for inputs not tried. Well-chosen cases make hidden errors much less likely.

Common mistakes

  • Calling a wrong answer a "syntax error."
  • Testing only with typical values.
  • Giving a test input without stating the expected result.
  • Assuming a program is correct because it runs.
  • Confusing overflow (too large to represent) with round-off (not exactly representable).
Check yourself

A program runs without crashing but calculates an average incorrectly. What kind of error is this?

  • Run-time error

  • Syntax error

  • Overflow error

  • Logic error

Show the answer

Logic error

The code is valid and executes, but does not do what was intended.

6For top marks

  • Name the type of error precisely and say why it is that type.
  • Pick boundary tests just below, at and above limits.
  • Explain how a test case reveals an error: state the input, expected output and actual output.
  • Trace code carefully with a table, especially loops and conditions.
Example

"Input [3, 8, 8]: expected count of 8s is 2, actual 1, so the loop stops one element early: change the loop to visit every element."

Check yourself

A test case should include:

  • Only the program name

  • An input and its expected output

  • Only the input

  • The compiler version

Show the answer

An input and its expected output

Compare expected and actual.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera