← IGCSE Computer Science Paper 2

Spotting common mistakes in standard algorithms

A mistake in an algorithm's steps that lets it run but gives wrong results, e.g. a total not set to 0, a loop going past the array end, or using > instead of <.

Understanding Spotting common mistakes in standard algorithms in IGCSE Computer Science Paper 2

What it does: Starting with the first item means Max is always a real value from the data, avoiding the error of Max starting at 0. The division goes after NEXT, once Total and Count are complete. Dividing inside the loop gives a wrong result each time. TO 11 makes the loop run past the end of the array, so Marks[11] does not exist. The loop bound must be the last index, 10. Using > keeps the largest value, so it finds the maximum instead. To find the minimum, replace Min whenever you meet something smaller (<). Starting Max at 0 only works if values are never negative. A safer start is the first array element, Max ← Temp[1].

Common mistakes: Total must start at 0. Starting at 1 adds an extra 1 to the answer every time. It should be Total ← 0. Temp holds 7 values (one per day), so the loop must stop at 7. Going to 8 reads past the end of the array. It should be FOR i ← 1 TO 7.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 296–306) and word search (Modules 296–316).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which is correct for a totalling algorithm?
Total ← 0 is written BEFORE the loopTotal ← 0 is written INSIDE the loop
Matches
IF Numbers[Index] > Min THEN Min ← Numbers[Index] ENDIF
↓
Wrong comparison: > is used when finding the minimum. It should be < so Min is replaced only by a smaller value.
Applying
Meanings
Term
logic error
Meaning
A mistake in an algorithm's steps that lets it run but gives wrong results, e.g. a total not set to 0, a loop going past the array end, or using > instead of <.
Practice
Total ← 0
Testing
Gaps
Total ←
Check Answer
Spots
1Total ← 1
2FOR i ← 1 TO 5

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Squirrel' badge — Module Hoarder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which is correct for a totalling algorithm?
A. Total ← 0 is written BEFORE the loop ✓
B. Total ← 0 is written INSIDE the loop
If Total is reset to 0 inside the loop, it is wiped on every repeat, so only the last item is left. Set it once before the loop starts.
2. An array Marks has 10 elements, numbered 1 to 10. Which loop header is correct?
A. FOR i ← 1 TO 11
B. FOR i ← 1 TO 10 ✓
TO 11 makes the loop run past the end of the array, so Marks[11] does not exist. The loop bound must be the last index, 10.
3. Which comparison correctly finds the MINIMUM value in a loop?
A. IF Num[i] < Min THEN ✓
B. IF Num[i] > Min THEN
Using > keeps the largest value, so it finds the maximum instead. To find the minimum, replace Min whenever you meet something smaller (<).
4. A student sets Max ← 0 before looping through temperatures that are all below zero. What happens?
A. Max stays 0, which is wrong because no temperature is larger than 0 ✓
B. Max correctly ends up as the highest temperature
Starting Max at 0 only works if values are never negative. A safer start is the first array element, Max ← Temp[1].
5. Where should the average be calculated?
A. Average ← Total / Count inside the loop, on every repeat
B. Average ← Total / Count after the loop has finished ✓
Dividing inside the loop gives a new value each time, using an incomplete total. It is also an error if Count is 0. Do the division once, after the loop.

Match the Pairs

Total ← 0 → Sets the running total to zero before the loop, so old or unknown values are not added to the answer
FOR Index ← 1 TO 10 (array has 10 items) → The loop bounds match the array size, so every item is visited and the loop never runs past the end
To find the maximum, start Max with the first array item, not 0, so negative numbers or low values still work → Max ← Numbers[1]
To calculate an average, divide once after the loop has finished, not inside the loop → NEXT Index Average ← Total / Count
IF Numbers[Index] > Min THEN Min ← Numbers[Index] ENDIF → Wrong comparison: > is used when finding the minimum. It should be < so Min is replaced only by a smaller value.
IF Names[Index] = Target THEN Found ← TRUE ENDIF (Index never updated, Found never set to FALSE first) → The Found flag is never set to FALSE before the loop and the position is not stored, so the result may be wrong or the location is lost.

Key Term

logic error
A mistake in an algorithm's steps that lets it run but gives wrong results, e.g. a total not set to 0, a loop going past the array end, or using > instead of <.

Fill in the Blanks

1. Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Mark[Index]
Hint: Type the starting value the total must have before the loop adds anything to it.
2. DECLARE Mark : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 5 Total ← Total + Mark[Index]
Hint: Type the final index of the array so the loop does not run past the end.
3. Min ← Score[1] FOR i ← 2 TO 5 IF Score[i] < Min THEN Min ← Score[i]
Hint: Type the comparison operator that finds a smaller value than the current minimum.
4. Total ← Total + Score[i] NEXT i Average ← Total / 5
Hint: Type the arithmetic operator for division, which is done once after the loop ends.
5. IF Name[i] = Target THEN Found ← TRUE ENDIF
Hint: Type the name of the flag variable that must be updated when the target is matched.

Spot the Error

Question 1
A Total ← 1
B FOR i ← 1 TO 5
C Total ← Total + Price[i]
D NEXT i
E OUTPUT Total
Line A — Total must start at 0. Starting at 1 adds an extra 1 to the answer every time. It should be Total ← 0.
Question 2
A Count ← 0
B FOR i ← 1 TO 8
C IF Temp[i] > 30 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Line B — Temp holds 7 values (one per day), so the loop must stop at 7. Going to 8 reads past the end of the array. It should be FOR i ← 1 TO 7.
Question 3
A Found ← FALSE
B FOR i ← 1 TO 5
C IF Names[i] = Target THEN
D Found ← FALSE
E ENDIF
F NEXT i
Line D — When the target is found, the flag must be updated to TRUE. Setting it to FALSE again means the search never reports a match. It should be Found ← TRUE.
Question 4
A Total ← 0
B Count ← 1
C FOR i ← 1 TO 5
D Total ← Total + Marks[i]
E Count ← Count + 1
F NEXT i
Line B — The counter must start at 0. Starting at 1 makes the count one too high, so any average using it will be wrong. It should be Count ← 0.
Question 5
A Count ← 0
B FOR i ← 1 TO 5
C IF Marks[i] >= 50 THEN
D Count ← Count + Marks[i]
E ENDIF
F NEXT i
Line D — Counting adds 1 each time the condition is true. Adding the mark turns the count into a total. It should be Count ← Count + 1.

Student with a class code? Join your class →