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.
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.
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 Mark[i] >= 50 THEN Count ← Count + 1Fill in the Blanks
Spot the Error
Student with a class code? Join your class →