Error Detection, Debugging & Algorithm Correction

A central objective of Cambridge IGCSE Paper 2 is assessing a student's ability to inspect algorithms, detect deliberate flaws, explain the nature of the error, and provide the exact corrected code.


1. The Three Error Classes

Error Class

Definition & Timing

Primary Cause

Classic Exam Example

Syntax Error

A violation of the formal grammar, spelling, or punctuation rules of the language. Prevented or caught before execution by the translator.

Misspelled keywords, missing closing structures, illegal characters.

Misspelling OUTPUT as OUPUT, omitting ENDIF, using single = for assignment.

Logic Error

The program is syntactically valid and executes without crashing, but generates incorrect results.

Flawed algorithmic reasoning, wrong mathematical formulas, inverted conditions.

Writing Area <- L + W instead of L * W. Calculating Average <- Total / 4 when 5 items were added.

Runtime Error

An illegal or impossible operation attempted while the program is actively running, causing an immediate crash.

Division by zero, accessing an array index outside allocated bounds.

Average <- Total / Count when Count = 0. Evaluating Array[6] on ARRAY[1:5].


2. Common Algorithm Bugs to Look For in Paper 2

  1. Incorrect Initialisation:
  • Total initialized inside the loop (resets every pass).
  • Min initialized to 0 (locks minimum at 0 for positive numbers).
  • Max initialized to 999 (locks maximum at 999).
  1. Off-by-One Loop Bounds:
  • Loops running 1 TO N - 1 when all $N$ items need processing.
  • Loops running 1 TO N during bubble sort comparisons, causing Array[i + 1] to access index $N + 1$ (Array Index Out of Bounds runtime crash).
  1. Loop Condition Inversion:
  • Using < instead of > or <= instead of >=.
  • In REPEAT ... UNTIL, using the continuation condition instead of the termination condition.
  1. Sentinel Inclusion:
  • Reading sentinel values and adding them to the total before testing the condition.

Written practice and self-review

Consider this flawed algorithm intended to calculate the sum of 10 input numbers:

1  FOR Counter <- 1 TO 10
2      Total <- 0
3      INPUT Number
4      Total <- Total + Number
5  NEXT Counter
6  OUTPUT "Final Total: ", Total
  1. Identify the line number containing the logic error and explain what problem it causes during execution.
  2. State how to correct this error.

Write an explanation before checking the theory. Check that each claim answers the question, uses the correct concept and explains why it applies. Discuss uncertain points with a teacher. The structured exercise below checks specific facts; it does not automatically mark this explanation.

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

Need guidance? Ask a question or use the hint below.

AI Socratic Tutor is an All-Access Benefit

Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.

You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.

Exam-Style Check: Classify Programming Error Types

+25 XP

Match each programming error example with its class:

Instructions: Click a term on the left, then click its corresponding definition on the right.
Terms / Concepts
Descriptions / Definitions

All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.