Programming Constructs and Operators: Question 6
Syllabus 8.1
A weather station technician enters a sequence of temperature readings, in degrees Celsius, one
at a time. The value -999 is used as a sentinel value to signal that there is no more data,
and is never itself a real temperature reading. This pseudocode algorithm totals the real
readings entered and counts how many of them there were, stopping as soon as the sentinel value
is read.
01 DECLARE Reading : INTEGER
02 DECLARE Total : INTEGER
03 DECLARE Count : INTEGER
04 Total ← 0
05 Count ← 0
06 INPUT Reading
07 WHILE Reading <> -999 DO
08 Total ← Total + Reading
09 Count ← Count + 1
10 INPUT Reading
11 ENDWHILE
12 OUTPUT Total
13 OUTPUT Count
(a) State whether this is a pre-condition loop or a post-condition loop, explaining your answer with reference to a specific line number. [2]
(b) The technician enters the readings 18, 22, -5, 31, -999, in that order. Complete a
trace table showing the value of Reading, Total and Count after each pass of the loop. [4]
(c) On a different day, the technician enters -999 as the very first reading (there is no
real data at all). State the values output for Total and Count in this case, explaining
why. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Pre-condition or post-condition?
A WHILE ... DO ... ENDWHILE loop is always a pre-condition loop: the condition is written
next to the WHILE keyword itself and is checked before the statements inside the loop are
ever executed.
In this algorithm, the condition Reading <> -999 is on line 07, and it is tested before
line 08 (Total ← Total + Reading) runs for the very first time. This is different from a
REPEAT ... UNTIL loop, where the condition is only checked after the body has already run
once. A REPEAT loop is guaranteed to execute its body at least once, but this WHILE loop is
not. [2 marks]: [1] for correctly identifying “pre-condition”, [1] for a correct
explanation referencing line 07 (or an equivalent explanation of the condition being checked
before the body).
Part (b): Tracing the WHILE loop
Total and Count both start at 0 (lines 04–05), and Reading is first given a value by the
priming read on line 06, before the loop is even reached.
| Pass | Reading (start of pass) | Reading <> -999 ? | Total | Count | Reading (re-read at end of pass) |
|---|---|---|---|---|---|
| , | 18 (from line 06) | , | 0 | 0 | - |
| 1 | 18 | true | 18 | 1 | 22 |
| 2 | 22 | true | 40 | 2 | -5 |
| 3 | -5 | true | 35 | 3 | 31 |
| 4 | 31 | true | 66 | 4 | -999 |
| , | -999 | false → loop stops | 66 | 4 | , |
[4 marks]: [1] for correctly tracing Total to a final value of 66, [1] for
correctly tracing Count to a final value of 4, [1] for correctly excluding the sentinel
value -999 from both Total and Count, [1] for a fully correct trace table showing all
four genuine passes.
Part (c): The edge case where the first reading is the sentinel
Line 06 performs the priming read, so Reading is set to -999 immediately, before the WHILE
condition on line 07 is tested for the first time. The test Reading <> -999 becomes
-999 <> -999, which is false. Because this is a pre-condition loop, the loop body (lines
08–10) is skipped entirely, it never runs, not even once.
Total and Count therefore keep the initial values assigned on lines 04 and 05:
Total = 0
Count = 0
[2 marks]: [1] for recognising that the loop body does not execute at all, [1] for
the correct final values Total = 0 and Count = 0.
Final answers
- (a) Pre-condition loop. The condition on line 07 is tested before line 08 first runs.
- (b) Trace as shown above.
Totalrises 18, 40, 35, 66;Countrises 1, 2, 3, 4. - (c)
Total = 0,Count = 0(the loop body never executes).