← IGCSE Computer Science Paper 2

Finding the maximum: how it works

Keep the largest value seen so far in Max. Start Max with the first value, then replace it whenever a later value is larger. Starting at 0 can fail if all values are negative.

Understanding Finding the maximum: how it works in IGCSE Computer Science Paper 2

What it does: Max already holds item 1, so the loop starts at 2 and compares every remaining value. This assignment inside the IF stores the new largest value so Max always holds the biggest seen so far. 9 is not larger than 14, so the IF Num > Max test is false. Max is only replaced when a larger value appears. Using < would keep the smaller value, which finds the minimum instead of the maximum. The maximum needs the test Num > Max.

Common mistakes: A FOR loop in Cambridge pseudocode sets its counter with the assignment arrow ←, not =. It should be FOR i ← 2 TO 5. The loop starts at 2 because Scores[1] is already in Max. Starting Max at 0 can fail. If every value is negative, none will beat 0 and the wrong answer stays in Max. Max should start with the first value, Max ← Ages[1].

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 286–295) and word search (Modules 275–295).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which is the best way to start a maximum search on a list of values?
Set Max to the last value you checkSet Max to the first value in the list
Matches
Max ← 0 FOR Index ← 1 TO 5 IF Numbers[Index] > Max THEN Max ← Numbers[Index] NEXT Index
↓
Starting Max at 0 fails when all values are negative. Max would stay 0, which is not in the list. Start with Max ← Numbers[1].
Applying
Meanings
Term
IF Num > Max THEN Max ← Num
Meaning
Keep the largest value seen so far in Max. Start Max with the first value, then replace it whenever a later value is larger. Starting at 0 can fail if all values are negative.
Practice
Max ← Numbers[1]
Testing
Gaps
Max ← Numbers[]
Check Answer
Spots
1Max ← Scores[1]
2FOR i = 2 TO 5

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

The 'Hedgehog' badge — First Steps, 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 the best way to start a maximum search on a list of values?
A. Set Max to the first value in the list ✓
B. Set Max to the last value you check
Starting with the first value means Max always holds a real value from the data. Every later value is then compared against it.
2. Max is 14 and the next value is 9. What happens to Max?
A. Max becomes 9
B. Max stays 14 ✓
9 is not larger than 14, so the IF Num > Max test is false. Max is only replaced when a larger value appears.
3. Max is 14 and the next value is 20. What happens to Max?
A. Max becomes 20 ✓
B. Max stays 14
20 > 14 is true, so Max ← Num runs. Max now holds the largest value seen so far.
4. Which line correctly replaces the maximum when a larger value is found?
A. IF Num < Max THEN Max ← Num
B. IF Num > Max THEN Max ← Num ✓
Using < would keep the smaller value, which finds the minimum instead of the maximum. The maximum needs the test Num > Max.
5. The values are -5, -2, -9. Max is set to 0 before the loop. What is the result?
A. Max stays 0, which is wrong because 0 is not in the data ✓
B. Max becomes -2, which is correct
No negative value is larger than 0, so Max never changes. The answer 0 is not in the list. Starting Max with the first value avoids this.

Match the Pairs

Max ← Numbers[1] → Start the maximum with the first value in the list, so it is a real value from the data
IF Numbers[Index] > Max THEN Max ← Numbers[Index] → Replace the largest value so far when a later value is bigger
Compare each later value with the largest so far by looping from the second item to the last → FOR Index ← 2 TO 5
Keep the largest value seen so far in a variable which is updated when a bigger value is found → Max ← Num
Max ← 0 FOR Index ← 1 TO 5 IF Numbers[Index] > Max THEN Max ← Numbers[Index] NEXT Index → Starting Max at 0 fails when all values are negative. Max would stay 0, which is not in the list. Start with Max ← Numbers[1].
IF Num < Max THEN Max ← Num → The wrong comparison is used. < finds a smaller value, so this finds the minimum. It should be IF Num > Max.

Key Term

IF Num > Max THEN Max ← Num
Keep the largest value seen so far in Max. Start Max with the first value, then replace it whenever a later value is larger. Starting at 0 can fail if all values are negative.

Fill in the Blanks

1. Max ← Numbers[1]
Hint: Type the index of the first value in the list, so Max starts with a real value from the data.
2. Max ← Numbers[1] FOR Index ← 2 TO 5
Hint: Type the index of the second value, because the first value is already stored in Max.
3. FOR Index ← 2 TO 5 IF Numbers[Index] > Max THEN Max ← Numbers[Index]
Hint: Type the comparison operator that checks whether the current value is larger than Max.
4. IF Numbers[Index] > Max THEN Max ← Numbers[Index] ENDIF
Hint: Type the loop variable that picks out the current element, which becomes the new largest value.
5. NEXT Index OUTPUT "Largest: ", Max
Hint: Type the variable that holds the largest value seen once the loop has finished.

Spot the Error

Question 1
A Max ← Scores[1]
B FOR i = 2 TO 5
C IF Scores[i] > Max THEN
D Max ← Scores[i]
E ENDIF
F NEXT i
Line B — A FOR loop in Cambridge pseudocode sets its counter with the assignment arrow ←, not =. It should be FOR i ← 2 TO 5. The loop starts at 2 because Scores[1] is already in Max.
Question 2
A Max ← 0
B FOR i ← 2 TO 6
C IF Ages[i] > Max THEN
D Max ← Ages[i]
E ENDIF
F NEXT i
Line A — Starting Max at 0 can fail. If every value is negative, none will beat 0 and the wrong answer stays in Max. Max should start with the first value, Max ← Ages[1].
Question 3
A Max ← Weights[1]
B FOR i ← 2 TO 6
C IF Weights[i] > Max THEN
D Max ← i
E ENDIF
F NEXT i
Line D — This stores the index i in Max, not the value found at that position. It should be Max ← Weights[i].
Question 4
A Max ← Heights[1]
B FOR i ← 2 TO 8
C IF Heights[i] < Max THEN
D Max ← Heights[i]
E ENDIF
F NEXT i
Line C — The < operator replaces Max with smaller values, so this finds the minimum. To find the maximum, the test must be Heights[i] > Max.
Question 5
A Highest ← Temp[1]
B FOR n ← 2 TO 7
C IF Temp[n] > Highest THEN
D Highest ← Temp[1]
E ENDIF
F NEXT n
Line D — Highest is reset to the first value every time instead of the larger value just found. It should be Highest ← Temp[n].

Student with a class code? Join your class →