← IGCSE Computer Science Paper 2

Counting only items that meet a condition

An IF inside a loop adds 1 to Count only when an item meets the condition, so Count tallies the matching items. A total adds the item's value instead.

Understanding Counting only items that meet a condition in IGCSE Computer Science Paper 2

What it does: The IF compares the current element with the string "Ali" and adds 1 to the counter only on a match. Counting uses Count ← Count + 1 for each match. This line adds the mark itself, so it totals instead of counting. The IF goes inside the loop, so Count only goes up when the condition is true. Without the IF, Count would go up for every item. If the counter is outside the IF, it counts every item, not just the matching ones. A condition compares with =. The ← arrow is for assignment, so it cannot be used in a condition.

Common mistakes: A mark of exactly 50 should be counted, so the condition must be >= 50. Using > 50 leaves out students who scored exactly 50. A FOR loop sets its start value with the assignment arrow, so it must be FOR i ← 1 TO 20. Writing = is a comparison and is not valid in a FOR header.

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 275–285) 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 loop body counts how many marks are 50 or more?
Count ← Count + 1 ENDIF IF Mark[i] >= 50 THENIF Mark[i] >= 50 THEN Count ← Count + 1 ENDIF
Matches
FOR i ← 1 TO 10 Count ← Count + 1 IF Mark[i] >= 50 THEN ENDIF NEXT i
↓
The counter is updated outside the IF, so every mark is counted, not just those that are 50 or more.
Applying
Meanings
Term
IF Mark[i] >= 50 THEN Count ← Count + 1
Meaning
An IF inside a loop adds 1 to Count only when an item meets the condition, so Count tallies the matching items. A total adds the item's value instead.
Practice
Count ← 0
Testing
Gaps
IF Mark[i] 50 THEN Count ← Count + 1
Check Answer
Spots
2FOR i ← 1 TO 30
3 IF Mark[i] > 50 THEN

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 loop body counts how many marks are 50 or more?
A. Count ← Count + 1 ENDIF IF Mark[i] >= 50 THEN
B. IF Mark[i] >= 50 THEN Count ← Count + 1 ENDIF ✓
The IF goes inside the loop, so Count only goes up when the condition is true. Without the IF, Count would go up for every item.
2. A student wants to count only the items that meet a condition. Where does Count ← Count + 1 go?
A. Inside the IF, so it only runs when the condition is true ✓
B. Outside the IF, so it runs on every loop
If the counter is outside the IF, it counts every item, not just the matching ones.
3. Which line counts the marks, and which line totals them? Pick the line that COUNTS marks of 50 or more.
A. Count ← Count + Mark[i]
B. Count ← Count + 1 ✓
A counter adds 1 each time. Adding Mark[i] adds the mark's value, which makes a total, not a count.
4. Which IF counts how many names in Name[] are "Ali"?
A. IF Name[i] = "Ali" THEN Count ← Count + 1 ✓
B. IF Name[i] ← "Ali" THEN Count ← Count + 1
A condition compares with =. The ← arrow is for assignment, so it cannot be used in a condition.
5. Which IF counts students who scored 50 or more in BOTH Maths and Science?
A. IF Maths[i] >= 50 AND Science[i] >= 50 THEN ✓
B. IF Maths[i] >= 50 OR Science[i] >= 50 THEN
AND needs both conditions to be true. OR would also count students who passed only one subject.

Match the Pairs

IF Mark[i] >= 50 THEN Count ← Count + 1 → Adds 1 to Count only when the current mark is 50 or more, so Count tallies the passing marks
IF Colour[i] = "Red" AND Size[i] > 10 THEN Count ← Count + 1 → Counts only the items that are red and also bigger than 10, because AND needs both conditions to be true
To count how many names are "Ali", add 1 to Count only when the current name matches the string → IF Name[i] = "Ali" THEN Count ← Count + 1
A counter goes up by 1 for each matching item, but a total goes up by the item's value → Total ← Total + Mark[i]
FOR i ← 1 TO 10 Count ← Count + 1 IF Mark[i] >= 50 THEN ENDIF NEXT i → The counter is updated outside the IF, so every mark is counted, not just those that are 50 or more.
IF Mark[i] >= 50 THEN Count ← Count + Mark[i] → This adds the mark's value to Count instead of 1, so it is making a total, not counting passes.

Key Term

IF Mark[i] >= 50 THEN Count ← Count + 1
An IF inside a loop adds 1 to Count only when an item meets the condition, so Count tallies the matching items. A total adds the item's value instead.

Fill in the Blanks

1. IF Mark[i] >= 50 THEN Count ← Count + 1
Hint: Type the comparison operator meaning 'greater than or equal to', so a mark of exactly 50 is counted too.
2. IF Name[i] = "Ali" THEN Count ← Count + 1
Hint: Type the comparison operator that checks whether the name is the same as the target string.
3. IF Mark[i] >= 50 AND Mark[i] < 70 THEN Count ← Count + 1 ENDIF
Hint: Type the logical operator that needs both conditions to be true before the counter goes up.
4. IF Mark[i] >= 50 THEN Total ← Total + Mark[i] ENDIF
Hint: A total adds each matching mark to a running sum. Type the variable name that stores that running sum.
5. IF Mark[i] >= 50 THEN Count ← Count + 1 ENDIF
Hint: Type the fixed number a counter goes up by each time an item meets the condition.

Spot the Error

Question 1
A Count ← 0
B FOR i ← 1 TO 30
C IF Mark[i] > 50 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Line C — A mark of exactly 50 should be counted, so the condition must be >= 50. Using > 50 leaves out students who scored exactly 50.
Question 2
A Count ← 0
B FOR i = 1 TO 20
C IF Answer[i] = "Yes" THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Line B — A FOR loop sets its start value with the assignment arrow, so it must be FOR i ← 1 TO 20. Writing = is a comparison and is not valid in a FOR header.
Question 3
A Count ← 0
B FOR i ← 1 TO 25
C IF Score[i] >= 40 THEN
D Count ← Count + Score[i]
E ENDIF
F NEXT i
Line D — This adds the score itself, so it builds a total instead of a count. A counter adds 1 for each matching item: Count ← Count + 1.
Question 4
A Count ← 0
B FOR i ← 1 UNTIL 7
C IF Temp[i] > 30 THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Line B — A FOR loop uses the keyword TO for its end value, so the line should be FOR i ← 1 TO 7. UNTIL belongs to REPEAT...UNTIL loops.
Question 5
A Count ← 0
B FOR i ← 1 TO 20
C IF Colour[i] ← "red" THEN
D Count ← Count + 1
E ENDIF
F NEXT i
Line C — The IF must compare the item with the string, so it needs = and not the assignment arrow. It should be IF Colour[i] = "red" THEN.

Student with a class code? Join your class →