Common Algorithms

Counting

Counting is the algorithm of finding how many items meet a criterion. A counter variable is incremented by 1 each time an item matches.

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).
  • IncrementCount <- Count + 1 inside 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: ", Count

Output: Even count: 5. The even numbers 2, 4, 6, 8, 10 each triggered the increment.

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".

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 conditionsMark >= 50, N MOD 2 = 0.
  • String conditionsAns = "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.

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.

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 indexIF 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.

14.6 Key Points Summary

A quick recap of everything covered on counting:

  • PurposeFind how many items satisfy a condition.
  • Initialise before loopCounter <- 0 BEFORE the loop, never inside.
  • Conditional incrementCount <- Count + 1 inside an IF that tests the criterion.
  • Output after loopPrint the final count AFTER the loop ends.
  • Flexible loop typeFOR, WHILE, or REPEAT — pick by task.
  • AND/OR allowedCombine multiple criteria in the IF condition.
  • Sentinel excludedThe stop value is NOT counted in input loops.
  • Arrays use FORFOR 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.

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/12 answered

Question 1Multiple Choice

What does the counting algorithm determine?

Question 2True / False

The counter variable must be initialised to 0 BEFORE the loop begins.

Question 3Multiple Choice

What is the final value of Count?

DECLARE Count : INTEGER
Count <- 0
FOR I <- 1 TO 10
  IF I MOD 2 = 0 THEN
    Count <- Count + 1
  ENDIF
NEXT I
OUTPUT Count

Question 4True / False

The counter increment statement "Count <- Count + 1" should be placed inside the IF branch that tests the criterion.

Question 5Multiple Choice

Which condition counts numbers strictly between 5 and 15 (exclusive)?

Question 6True / False

A sentinel value used to terminate input should also be counted if it matches the criterion.

Question 7Multiple Choice

How many positive numbers are counted?

DECLARE V, Count : INTEGER
Count <- 0
INPUT V
WHILE V <> 0 DO
  IF V > 0 THEN
    Count <- Count + 1
  ENDIF
  INPUT V
ENDWHILE
OUTPUT Count
// Inputs: 7, -2, 5, 0

Question 8True / False

When counting in an array of size N, a FOR loop should iterate from 1 to N.

Question 9Multiple Choice

What is wrong with this counting code?

FOR I <- 1 TO 5
  Count <- 0
  IF A[I] > 10 THEN
    Count <- Count + 1
  ENDIF
NEXT I
OUTPUT Count

Question 10True / False

You can count multiple criteria in a single loop using separate counters (e.g. CountPass and CountFail).

Question 11Multiple Choice

Which problem BEST fits the counting algorithm?

Question 12True / False

The OUTPUT statement that prints the final count should be placed INSIDE the loop so the user sees the running total.

Answer all 12 questions to enable submission.