14.1 What is Counting?
Counting is the algorithm of finding how many items satisfy a condition. A counter variable starts at 0 and is incremented by 1 each time an item matches the criterion.
- Counter variable — INTEGER, initialised to 0 before the loop.
- Criterion — a condition tested on each item (e.g.
> 50). - Increment —
Count <- Count + 1inside an IF. - Output — the final count is printed AFTER the loop.
Key idea: counting answers "how many?". Totalling answers "how much?". The two are easy to confuse — counting uses +1, totalling uses +value.
// Count how many numbers from 1 to 10 are even
DECLARE Count : INTEGER
Count <- 0
FOR I <- 1 TO 10
IF I MOD 2 = 0 THEN
Count <- Count + 1
ENDIF
NEXT I
OUTPUT "Even count: ", CountOutput: Even count: 5. The even numbers 2, 4, 6, 8, 10 each triggered the increment.
Concepts — What is Counting?
14.2 The Counter Pattern
The counter pattern has four steps: initialise the counter to 0, loop over the items, increment inside an IF when the criterion is true, and output the final count after the loop.
// General counter pattern
DECLARE Count : INTEGER
Count <- 0 // 1. INITIALISE before the loop
FOR I <- 1 TO N
IF <criterion> THEN
Count <- Count + 1 // 2. INCREMENT inside IF
ENDIF
NEXT I
OUTPUT "Count: ", Count // 3. OUTPUT after the loop- Initialise ONCE — counter <- 0 before the loop, never inside.
- Increment conditionally — inside an IF that tests the criterion.
- OUTPUT after loop — to print the final count, not the running count.
- Loop type flexible — FOR, WHILE, or REPEAT all work.
Common mistake: writing Count <- 0 inside the loop. This resets the counter each pass — the final count will be 0 or 1, never the true total.
Exam tip: if a question says "count how many" or "find the number of", that is the counter pattern. Look for the keyword "how many".
The Counter Pattern — Step by Step
14.3 Counting with Conditions
The criterion in the IF can be anything that produces a BOOLEAN — numeric, string, or combined with AND/OR. You can also run multiple counters in a single loop.
// Count passes (>= 50) AND failures (< 50) together
DECLARE Mark : INTEGER
DECLARE PassCount, FailCount : INTEGER
PassCount <- 0
FailCount <- 0
FOR I <- 1 TO 30
INPUT Mark
IF Mark >= 50 THEN
PassCount <- PassCount + 1
ELSE
FailCount <- FailCount + 1
ENDIF
NEXT I
OUTPUT "Passed: ", PassCount
OUTPUT "Failed: ", FailCount- Numeric conditions —
Mark >= 50,N MOD 2 = 0. - String conditions —
Ans = "Yes",Name[1] = "A". - Combined with AND/OR —
(A >= 50) AND (A <= 70). - Multiple counters — separate IF branches, each with its own counter.
Rule of thumb: when you need to count two mutually-exclusive cases (pass/fail, even/odd), use IF...ELSE with one counter in each branch. For overlapping cases, use two separate IFs.
Counting with Conditions — One or More
14.4 Counting with Input
When the items come from the user, a sentinel loop reads values until a stop signal is entered. The counter increments inside the loop whenever the criterion is met.
// Count how many positive numbers the user enters (0 to stop)
DECLARE V : INTEGER
DECLARE PosCount : INTEGER
PosCount <- 0
INPUT V // prime read
WHILE V <> 0 DO
IF V > 0 THEN
PosCount <- PosCount + 1
ENDIF
INPUT V // read next value
ENDWHILE
OUTPUT "Positive count: ", PosCount- Prime read — first INPUT before the WHILE, so the condition has a value to test.
- Inner INPUT — read the next value inside the loop body.
- Sentinel excluded — the stop value (0) is NOT counted.
- WHILE preferred — because the body may need to be skipped entirely.
Watch out: forgetting the inner INPUT creates an infinite loop — the same value is tested forever. Always read the next value inside the loop body.
Exam tip: "Read numbers until 0 is entered" signals a sentinel WHILE loop. "The user must be prompted at least once" signals REPEAT...UNTIL instead.
Counting with Input — Sentinel Loops
14.5 Counting in Arrays
Arrays pair naturally with FOR loops for counting. Loop through each element by index, test the criterion inside an IF, and increment the counter on each match.
// Count how many of 8 stored marks are above the pass threshold
DECLARE Mark : ARRAY[1:8] OF INTEGER
DECLARE Count : INTEGER
DECLARE I : INTEGER
// (assume Mark[1..8] already populated)
Count <- 0
FOR I <- 1 TO 8
IF Mark[I] >= 50 THEN
Count <- Count + 1
ENDIF
NEXT I
OUTPUT "Number passed: ", Count- 1-based indexing — CIE arrays declared
ARRAY[1:N]start at index 1. - FOR I <- 1 TO N — visits every element exactly once.
- Test element by index —
IF A[I] > 10. - Counter still starts at 0 — same rule as everywhere else.
Pattern: the counter pattern is the same whether you iterate an array, read input, or use a fixed FOR range. Only the loop type changes — the four steps stay constant.
Counting in Arrays — Indexed Iteration
14.6 Key Points Summary
A quick recap of everything covered on counting:
- Purpose — Find how many items satisfy a condition.
- Initialise before loop — Counter <- 0 BEFORE the loop, never inside.
- Conditional increment — Count <- Count + 1 inside an IF that tests the criterion.
- Output after loop — Print the final count AFTER the loop ends.
- Flexible loop type — FOR, WHILE, or REPEAT — pick by task.
- AND/OR allowed — Combine multiple criteria in the IF condition.
- Sentinel excluded — The stop value is NOT counted in input loops.
- Arrays use FOR — FOR I <- 1 TO N to visit every element by index.
Exam tip: look for the keyword "how many" or "number of". That always means counting. Don't confuse it with totalling, which uses + value rather than + 1.