Unable to open this exercise
Common exam pitfall
Students often confuse logic errors (program runs but gives wrong answer) with runtime errors (program crashes during execution).
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 |
Logic Error | The program is syntactically valid and executes without crashing, but generates incorrect results. | Flawed algorithmic reasoning, wrong mathematical formulas, inverted conditions. | Writing |
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. |
|
2. Common Algorithm Bugs to Look For in Paper 2
- Incorrect Initialisation:
Totalinitialized inside the loop (resets every pass).Mininitialized to0(locks minimum at 0 for positive numbers).Maxinitialized to999(locks maximum at 999).
- Off-by-One Loop Bounds:
- Loops running
1 TO N - 1when all $N$ items need processing. - Loops running
1 TO Nduring bubble sort comparisons, causingArray[i + 1]to access index $N + 1$ (Array Index Out of Bounds runtime crash).
- Loop Condition Inversion:
- Using
<instead of>or<=instead of>=. - In
REPEAT ... UNTIL, using the continuation condition instead of the termination condition.
- 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
- Identify the line number containing the logic error and explain what problem it causes during execution.
- 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
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
Match each programming error example with its class:
Interactive exercises for Module 2 and beyond require an active plan.