← IGCSE Computer Science Paper 2

Combining sequence, selection and iteration in one algorithm

Set counters to 0 first (sequence), loop to read input (iteration), use IF inside the loop to pick values to count or total (selection), then OUTPUT the result after the loop.

Understanding Combining sequence, selection and iteration in one algorithm in IGCSE Computer Science Paper 2

What it does: A FOR loop repeats the INPUT 30 times, which is the iteration part of the algorithm. The OUTPUT comes after NEXT, so it runs once when all the data has been processed rather than on every repeat. Selection inside the loop picks only the values you want to count. Without the IF, every mark would be counted. Total must be set to 0 before the loop. If you set it afterwards, the total is wiped out before it is output. MOD gives the remainder, so Number MOD 2 = 0 means the number is even. DIV gives the whole-number part of a division, which is not what we need here.

Common mistakes: Cambridge pseudocode reads a value with INPUT, not READ. The line should be INPUT Price, so each pass of the loop reads one price. An IF statement needs THEN after its condition. The line should be IF Temp > 30 THEN, so the selection decides which temperatures are added to Total.

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 389–398) and word search (Modules 379–398).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Where should Count <- 0 be written in an algorithm that counts passes in a FOR loop?
Before the loop startsInside the loop, on the first line
Matches
FOR i <- 1 TO 10 Count <- 0 INPUT Num IF Num > 5 THEN Count <- Count + 1 ENDIF NEXT i
↓
Count is set to 0 inside the loop, so it is reset on every repeat and the final count is wrong. It must be set to 0 before the loop.
Applying
Meanings
Term
sequence, selection, iteration
Meaning
Set counters to 0 first (sequence), loop to read input (iteration), use IF inside the loop to pick values to count or total (selection), then OUTPUT the result after the loop.
Practice
Count ← 0
Testing
Gaps
Total ←
Check Answer
Spots
2FOR i <- 1 TO 5
3 READ Price

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

The 'Sperm Whale' badge — Deep Sea Champion, 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. Where should Count <- 0 be written in an algorithm that counts passes in a FOR loop?
A. Before the loop starts ✓
B. Inside the loop, on the first line
Setting a counter to 0 is sequence. It must happen once before the loop. If it is inside the loop, it resets to 0 on every repeat.
2. An algorithm must count how many of 30 marks are 50 or more. Which structure goes inside the loop?
A. IF Mark >= 50 THEN Count <- Count + 1 ENDIF ✓
B. OUTPUT Count <- Count + 1 for every mark
Selection inside the loop picks only the values you want to count. Without the IF, every mark would be counted.
3. Which statement is true about the three constructs in a complete algorithm?
A. Selection repeats the input, iteration sets the counters, sequence decides which values to total
B. Sequence sets up and outputs, iteration repeats, selection decides inside the loop ✓
Sequence gives the ordered steps for setup and output. Iteration repeats. Selection (IF) chooses what happens each time round the loop.
4. Where should OUTPUT "Passes: ", Count go if you want the final total of passes?
A. After ENDFOR, once the loop has finished ✓
B. Inside the loop, before NEXT
Results are output after the loop, so the final value is shown once. Inside the loop it would print a partial total on every repeat.
5. Which is the correct order of the parts of a totalling algorithm?
A. Loop with INPUT and IF, Total <- 0, then OUTPUT Total
B. Total <- 0, loop with INPUT and IF, then OUTPUT Total ✓
Total must be set to 0 before the loop. If you set it afterwards, the total is wiped out before it is output.

Match the Pairs

Count <- 0 Total <- 0 → Sequence: set the counter and total to 0 before the loop starts
IF Mark >= 50 THEN Passes <- Passes + 1 ENDIF → Selection inside the loop: only count the values that meet the condition
Iteration repeats the input and processing steps a set number of times → FOR Student <- 1 TO 30 INPUT Mark NEXT Student
Results are output after the loop has finished, as the last part of the sequence → NEXT Student OUTPUT "Passes: ", Passes
FOR i <- 1 TO 10 Count <- 0 INPUT Num IF Num > 5 THEN Count <- Count + 1 ENDIF NEXT i → Count is set to 0 inside the loop, so it is reset on every repeat and the final count is wrong. It must be set to 0 before the loop.
FOR i <- 1 TO 10 INPUT Num Total <- Total + Num OUTPUT Total NEXT i → OUTPUT is inside the loop, so it prints ten times. The result should be output once after NEXT.

Key Term

sequence, selection, iteration
Set counters to 0 first (sequence), loop to read input (iteration), use IF inside the loop to pick values to count or total (selection), then OUTPUT the result after the loop.

Fill in the Blanks

1. Total ← 0 FOR Index ← 1 TO 5 INPUT Mark
Hint: Type the starting value the total must have before the loop begins.
2. FOR Student ← 1 TO 30 INPUT Mark IF Mark >= 50 THEN
Hint: Type the keyword that starts a selection inside the loop to choose which marks to count.
3. IF Mark >= 50 THEN Passes ← Passes + 1 ENDIF
Hint: Type the arithmetic operator that increases the counter each time the condition is met.
4. NEXT Index OUTPUT "Total: ", Total
Hint: Type the name of the variable that holds the running sum, to be output after the loop has finished.
5. IF Mark > 50 THEN Total ← Total + Mark ENDIF
Hint: Type the variable holding the value just input that is added to the total when selected.

Spot the Error

Question 1
A Total <- 0
B FOR i <- 1 TO 5
C READ Price
D IF Price < 10 THEN
E Total <- Total + Price
F ENDIF
Line C — Cambridge pseudocode reads a value with INPUT, not READ. The line should be INPUT Price, so each pass of the loop reads one price.
Question 2
A Total <- 0
B FOR i <- 1 TO 8
C INPUT Temp
D IF Temp > 30
E Total <- Total + Temp
F ENDIF
Line D — An IF statement needs THEN after its condition. The line should be IF Temp > 30 THEN, so the selection decides which temperatures are added to Total.
Question 3
A Count = 0
B FOR i <- 1 TO 20
C INPUT Age
D IF Age >= 18 THEN
E Count <- Count + 1
F ENDIF
Line A — Assignment uses <-, not =. In Cambridge pseudocode = is a comparison. The counter must be set with Count <- 0 before the loop starts.
Question 4
A IF Mark => 50 THEN
B Pass <- Pass + 1
C ENDIF
D NEXT i
E OUTPUT Pass
Line A — There is no => operator. 'Greater than or equal to' is written >=, so the line should be IF Mark >= 50 THEN.
Question 5
A FOR i <- 1 TO 10
B INPUT Mark
C IF Mark >= 50 THEN
D Count <- Count + Mark
E ENDIF
F NEXT i
Line D — To count the passes, Count must go up by 1 each time, not by the mark. The line should be Count <- Count + 1. Adding Mark would be totalling.

Student with a class code? Join your class →