← IGCSE Computer Science Paper 2

Counting elements that meet a condition

An IF inside the loop tests each element and adds 1 to Count when true. Set Count to 0 before the loop. A counter counts matches; a total adds up values.

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

What it does: The condition compares the element Names[Index] with the name being searched for, and the counter increases by 1 on each match. A counter uses Count + 1 inside an IF, while a total adds the value Marks[Index] to Total, so this line is a total and not a counter. Count must be set to 0 once, before the loop. If it were reset inside the loop, it would go back to 0 on every repeat and lose the count. Count only goes up when the IF test is true, so it ends at the number of matches (6), not the number of elements checked.

Common mistakes: The IF line is missing THEN. It must be IF Marks[Index] >= 50 THEN so the counter is only increased when the test is true. Sam is text, so it needs quote marks. Without them it looks like a variable. It should be IF Names[Index] = "Sam" THEN.

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 459–468) and word search (Modules 459–478).

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 when counting how many marks are 50 or more?
Before the loop startsInside the loop, before the IF
Matches
FOR Index ← 1 TO 10 Count ← 0 IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF NEXT Index
↓
Count ← 0 is inside the loop, so the counter is reset to zero on every repeat and the final count is wrong. Set it to 0 once before the loop.
Applying
Meanings
Term
IF Marks[Index] >= 50 THEN Count ← Count + 1
Meaning
An IF inside the loop tests each element and adds 1 to Count when true. Set Count to 0 before the loop. A counter counts matches; a total adds up values.
Practice
Count ← 0
Testing
Gaps
Count ←
Check Answer
Spots
2FOR Index ← 1 TO 10
3 IF Marks[Index] >= 50

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

The 'Leopard' badge — Savannah Perfectionist, 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 when counting how many marks are 50 or more?
A. Before the loop starts ✓
B. Inside the loop, before the IF
Count must be set to 0 once, before the loop. If it were reset inside the loop, it would go back to 0 on every repeat and lose the count.
2. Which line adds 1 to the counter when a mark passes the test?
A. Count ← Count + 1 ✓
B. Count ← Marks[Index]
A counter goes up by 1 for each match. Count ← Marks[Index] would overwrite the counter with a mark.
3. Which code counts the marks that are 50 or more?
A. IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF ✓
B. IF Marks >= 50 THEN Count ← Count + 1 ENDIF
The test must look at one element using an index, Marks[Index]. Marks on its own is the whole array, not one value.
4. A loop checks 10 marks and 6 of them are 50 or more. What is Count at the end?
A. 10
B. 6 ✓
Count only goes up when the IF test is true, so it ends at the number of matches (6), not the number of elements checked.
5. What is the difference between a counter and a total?
A. A counter adds up the values; a total adds 1 for each match
B. A counter adds 1 for each match; a total adds up the values ✓
Count ← Count + 1 counts matches. Total ← Total + Marks[Index] adds up the values themselves.

Match the Pairs

Count ← 0 → Sets the counter to zero before the loop starts, so counting begins from nothing
IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF → Tests the current element and adds 1 to Count only when the mark is 50 or more
To count how many names in an array equal a given name, compare each element with that name and add 1 when they match → IF Names[Index] = Target THEN Count ← Count + 1 ENDIF
A counter adds 1 each time the test is true, but a total adds the element values themselves → Total ← Total + Marks[Index]
FOR Index ← 1 TO 10 Count ← 0 IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF NEXT Index → Count ← 0 is inside the loop, so the counter is reset to zero on every repeat and the final count is wrong. Set it to 0 once before the loop.
IF Marks[Index] >= 50 THEN Count ← Count + Marks[Index] ENDIF → Count adds the mark itself instead of 1, so it totals the passing marks rather than counting them. It should be Count ← Count + 1.

Key Term

IF Marks[Index] >= 50 THEN Count ← Count + 1
An IF inside the loop tests each element and adds 1 to Count when true. Set Count to 0 before the loop. A counter counts matches; a total adds up values.

Fill in the Blanks

1. Count ← 0 FOR Index ← 1 TO 10 IF Marks[Index] >= 50 THEN
Hint: Type the starting value the counter is given before the loop begins
2. FOR Index ← 1 TO 10 IF Marks[Index] >= 50 THEN Count ← Count + 1
Hint: Type the comparison operator meaning 'greater than or equal to'
3. Count ← Count + 1 ENDIF NEXT Index
Hint: Type the keyword that closes the selection statement inside the loop
4. IF Names[Index] = SearchName THEN Count ← Count + 1 ENDIF
Hint: Type the operator that tests whether two values are equal
5. IF Marks[Index] >= 50 THEN Count ← Count + 1 ENDIF
Hint: Type the fixed amount a counter goes up by each time a match is found

Spot the Error

Question 1
A Count ← 0
B FOR Index ← 1 TO 10
C IF Marks[Index] >= 50
D Count ← Count + 1
E ENDIF
F NEXT Index
Line C — The IF line is missing THEN. It must be IF Marks[Index] >= 50 THEN so the counter is only increased when the test is true.
Question 2
A Count ← 0
B FOR Index ← 1 TO 20
C IF Names[Index] = Sam THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Line C — Sam is text, so it needs quote marks. Without them it looks like a variable. It should be IF Names[Index] = "Sam" THEN.
Question 3
A Count ← 0
B FOR Index ← 1 UNTIL 10
C IF Marks[Index] >= 50 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Line B — A count-controlled loop uses TO, not UNTIL. The line should be FOR Index ← 1 TO 10.
Question 4
A Count ← 1
B FOR Index ← 1 TO 8
C IF Temp[Index] > 30 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Line A — The counter must start at 0 before the loop. Starting at 1 makes the final count one too high. It should be Count ← 0.
Question 5
A Count ← 0
B FOR Index ← 1 TO
C IF Age[Index] >= 18 THEN
D Count ← Count + 1
E ENDIF
F NEXT Index
Line B — The FOR loop has no upper bound, so it does not say where to stop. It should be FOR Index ← 1 TO 5 so every element is tested.

Student with a class code? Join your class →