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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
IF Marks[Index] >= 50 THEN Count ← Count + 1Fill in the Blanks
Spot the Error
Student with a class code? Join your class →