AS · Practice questions · Selection and iteration constructs

Pick the right loop.

Six original Cambridge-style questions on selection and iteration in 9618 pseudocode, with mark points.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Keep these straight

Test first or test last.

01
Explain
[3 marks]

Explain the difference between a pre-condition loop and a post-condition loop, giving a suitable use for each.

  • A pre-condition loop tests its condition before the body, so the body may run zero times. ✓
  • A post-condition loop tests its condition after the body, so the body always runs at least once. ✓
  • Uses: pre-condition for reading a file that may be empty (WHILE NOT EOF); post-condition for validating input that must be entered at least once. ✓
02
Trace
[4 marks]

The inputs are 4, 7, 10, 0, 3. Complete a trace table and state the output.

Total ← 0
Count ← 0
REPEAT
    INPUT Value
    IF Value MOD 2 = 0 THEN
        Total ← Total + Value
    ENDIF
    Count ← Count + 1
UNTIL Value = 0 OR Count = 5
OUTPUT Total, " ", Count
  • Value 4: even, Total 4, Count 1; Value 7: odd, Total 4, Count 2. ✓
  • Value 10: even, Total 14, Count 3. ✓
  • Value 0: 0 MOD 2 = 0, so Total stays 14 (adds 0), Count 4; Value = 0 is TRUE so the loop stops. ✓
  • Output: 14 4 (the input 3 is never read). ✓
03
Write
[4 marks]

Day holds an integer. Write a CASE structure that outputs Weekday for 1 to 5, Weekend for 6 or 7 and Invalid day for any other value.

  • CASE OF Day ... ENDCASE ✓
  • 1 TO 5 : OUTPUT "Weekday" ✓
  • 6 TO 7 : OUTPUT "Weekend" (or two clauses 6 and 7) ✓
  • OTHERWISE : OUTPUT "Invalid day" as the last clause. ✓
04
Write
[5 marks]

A museum charges 5.00 for visitors under 12, 7.00 for ages 12 to 17 and 10.00 for ages 18 and over, except that students aged 18 or over pay 8.00. Write pseudocode using nested IF statements that inputs Age (INTEGER) and Student (BOOLEAN) and outputs the price.

  • INPUT Age and INPUT Student. ✓
  • IF Age < 12 THEN Price ← 5.00 ELSE ... ✓
  • nested IF Age <= 17 THEN Price ← 7.00 ELSE ... ✓
  • nested IF Student = TRUE THEN Price ← 8.00 ELSE Price ← 10.00, with three correctly matched ENDIFs. ✓
  • OUTPUT Price. ✓
05
Analysis
[3 marks]

Choose the most suitable loop for each task and justify your choice.

(a) Output the 12 times table from 1 × 12 to 12 × 12.
(b) Keep asking for a PIN until the correct PIN is entered.
(c) Halve a value until it is below 1, where the value input may already be below 1.

  • (a) FOR loop: the number of repetitions (12) is known in advance. ✓
  • (b) REPEAT ... UNTIL: the PIN must be input at least once before it can be checked. ✓
  • (c) WHILE loop: the condition is tested first, so if the value is already below 1 the body is not run. ✓
06
Convert
[4 marks]

Rewrite this loop using a WHILE loop so that it produces the same result, and state the final value of Total.

Total ← 0
FOR Index ← 1 TO 20 STEP 2
    Total ← Total + Index
NEXT Index
  • Total ← 0 and Index ← 1 before the loop. ✓
  • WHILE Index <= 20 ... ENDWHILE. ✓
  • Inside: Total ← Total + Index then Index ← Index + 2. ✓
  • Total = 1 + 3 + 5 + ... + 19 = 100. ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.