Cambridge IGCSE · Topic 7 (7-9)
IGCSE Computer Science 0478 / Topic 7 / IGCSE

Spot the bug.

Exam papers often give you an algorithm that almost works, with a few lines wrong. Your job is to find each error, give its line number and write the corrected line. Most errors fall into a handful of familiar types, so learn to look for them.

The key idea

To find errors, first work out the algorithm's intended purpose, then dry-run it with simple test data and compare the actual result with the expected result. Common errors are a wrong operator (< for >), counting instead of totalling, a wrong initial value or one set inside the loop, wrong loop bounds, a missing input inside a loop and a swap without a temporary variable. For each error give the line number and the corrected line of pseudocode.

Intended: output the total of 10 numbers 1 Total ← 0 2 FOR Count ← 1 TO 10 3 INPUT Number 4 Total ← Total + 1 5 NEXT Count 6 OUTPUT Total Total ← Total + Number line 4 counts instead of adding
Fig. 1: line 4 adds 1 instead of the number input, so the algorithm counts instead of totalling
Section 01

Where errors hide.

Try the find-the-error tab in the lab. Each program has a stated purpose and a mistake; run it with test data, see the wrong result, then fix the line.

Error typeFaulty lineCorrected line
Counting instead of totallingTotal ← Total + 1Total ← Total + Mark
Wrong relational operatorIF Mark < HighestIF Mark > Highest
Initial value inside the loopTotal ← 0 inside the FOR loopMove Total ← 0 before the loop
Wrong loop boundsFOR Count ← 0 TO 10 (11 passes)FOR Count ← 1 TO 10
Missing input in a loopNo INPUT before ENDWHILEAdd INPUT Number inside the loop
Wrong divisorAverage ← Total / 9 for 10 valuesAverage ← Total / 10
Examiner trap

"Line 5 is wrong" earns no mark on its own. Give the line number and the full corrected statement in pseudocode.

Section 02

A worked example.

This algorithm should input 10 positive numbers, then output the highest number and the average. It contains three errors.

01 Highest ← 0
02 Total ← 0
03 FOR Count ← 1 TO 10
04     INPUT Number
05     Total ← Total + 1
06     IF Number < Highest
07       THEN
08         Highest ← Number
09     ENDIF
10 NEXT Count
11 Average ← Total / 9
12 OUTPUT Highest, " ", Average
Worked example Find and correct the errors
Identify the three errors and write the corrected lines.
  1. Dry-run with simple data, e.g. ten 5s: Total ends as 10 (should be 50) and Highest stays 0 (should be 5).
  2. Line 05 counts instead of totalling. Correct: Total ← Total + Number
  3. Line 06 tests for a smaller number, so Highest never changes from 0. Correct: IF Number > Highest
  4. Line 11 divides by 9, but there are 10 numbers. Correct: Average ← Total / 10
Answer: Lines 05, 06 and 11, corrected as shown
Examiner trap

Do not rewrite the whole algorithm. Change only the faulty lines, and keep the original variable names.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is the best first step when looking for errors in an algorithm?

Quick check+10 XP

An algorithm meant to total marks has Total ← Total + 1. What does it actually calculate?

Quick check+10 XP

Why is Total ← 0 inside a FOR loop an error?

Quick check+10 XP

A loop is written FOR Count ← 0 TO 10 but should run 10 times. How many times does it run?

Section 03

Errors that stop a loop.

Some errors make a loop run the wrong number of times, or never stop. Ask: what changes the loop condition, and does it ever change?

01 Total ← 0
02 OUTPUT "Enter a number, -1 to stop"
03 INPUT Number
04 WHILE Number <> -1 DO
05     Total ← Total + Number
06 ENDWHILE
07 OUTPUT Total
Your turn
Explain why this algorithm never stops if the first number is 5, and correct it.
Your turn
A bubble sort swaps with Values[I] ← Values[I + 1] then Values[I + 1] ← Values[I]. What goes wrong, and what is the fix?
Examiner trap

A loop condition that uses a variable never changed inside the loop is a warning sign: either the loop never runs, or it never ends.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

An algorithm should find the smallest of 20 values but starts with Smallest ← 0. All values are positive. What is output?

Exam style+20 XP

Which change corrects WHILE Number = -1 DO in a loop meant to stop when -1 is entered?

Exam style+20 XP

A WHILE loop processes inputs until -1, but never asks for another input inside the loop. What happens if the first input is 8?

Exam style+20 XP

Which line correctly averages 12 values stored in Total?

Skill unlocked

Finding errors, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Finding errors in given algorithms by dry-running them, and writing the corrected pseudocode lines.