Totalling, Counting & Sentinel Input Loops

Two of the most frequently examined standard algorithmic operations in Cambridge IGCSE Computer Science are Totalling (accumulating sums) and Counting (tracking occurrences).


1. Totalling vs Counting

Characteristic

Totalling (Accumulator)

Counting (Counter)

Purpose

Computes the running sum of a series of numerical inputs.

Tracks how many times an event occurred or items processed.

Typical Initialization

Total <- 0

Count <- 0

Update Assignment

Total <- Total + ItemValue

Count <- Count + 1

Value Added

Variable: Changes with each data item entered.

Constant: Always increments by a fixed step (usually 1).


2. Sentinel Values and Rogue Values

When the exact number of items to be entered is unknown in advance, a Sentinel Value (also called a rogue value) is used to terminate input:

  • Common examples: -1 (for positive numbers), 999, "XXX", or "STOP".
  • Exam Rule: The sentinel value must never be included in the calculation or added to the counter!

The Priming Read Pattern:

To prevent the sentinel value from polluting the accumulator, developers use the "priming read" pattern:

Total <- 0
Count <- 0

INPUT Value             // 1. Priming read before loop starts

WHILE Value <> -1 DO    // 2. Test condition BEFORE processing
    Total <- Total + Value
    Count <- Count + 1
    INPUT Value         // 3. Read subsequent value at end of loop body
ENDWHILE

If the user enters -1 immediately on the very first prompt, the WHILE condition evaluates to FALSE, the loop never executes, and Total correctly remains 0.

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

Need guidance? Ask a question or use the hint below.

AI Socratic Tutor is an All-Access Benefit

Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.

You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.

Independent Practice: Positive readings

+30 XP

Read integers until -1. Ignore other non-positive values. Output the total and then the count of positive values. Do not include the sentinel.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.

solution.pseudo
All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.

○ Mixed readings
○ No readings
○ Single reading