Use pseudocode to write: a 'pre-condition' loop

Topic 11.2 – Constructs: Pre‑condition Loops

Learning Outcomes (Cambridge 9618)

  • Write correct pseudocode for a pre‑condition loop using the Cambridge syntax WHILE … DO … END WHILE.
  • Analyse when a WHILE loop is the most appropriate choice and compare it with REPEAT‑UNTIL and FOR loops.
  • Translate the WHILE loop into a permitted language (Java, Python, Visual Basic) and produce a concise test plan.
  • Evaluate loop correctness, avoid infinite loops and handle edge‑cases such as empty or invalid input.

Syllabus & Assessment Objective Mapping

Syllabus SectionContent CoveredAO1 (Knowledge)AO2 (Application/Analysis)AO3 (Evaluation)
11.2 Constructs – LoopsPre‑condition (WHILE) loops, syntax, characteristics, pitfalls
11.3 Structured ProgrammingEmbedding loops in larger algorithms (e.g., summing a series)
13 Programming – Practical Component (Paper 4)Translate WHILE‑loop pseudocode into Java/Python, test, document

What Is a Pre‑condition Loop?

A pre‑condition loop repeats a block of statements while a given Boolean condition is true. The condition is evaluated before each iteration, so the loop body may execute zero times if the condition is false initially.

Cambridge Pseudocode Syntax

WHILE condition DO

statements

END WHILE

Cambridge Pseudocode Cheat‑Sheet (AO1)

KeywordPurposeExact spelling / punctuation
DECLAREIntroduce one or more variablesDECLARE var1, var2 AS TYPE (comma‑separated, single space before AS)
SETAssign a valueSET var ← expression (arrow symbol, spaces around it)
READInput from the userREAD var
OUTPUTDisplay a value or messageOUTPUT "text", var (comma separates items)
WHILE … DOStart of a pre‑condition loopAll caps, single space before and after the condition
END WHILETerminate the loopTwo words, both caps, separated by a space

Pseudocode Checklist (AO2)

  • All keywords in UPPERCASE.
  • Boolean condition uses relational operators (≤, ≥, =, ≠, <, >).
  • Indent the loop body consistently (4 spaces or a tab).
  • Declare every variable before it is used.
  • Update at least one loop‑control variable inside the body.
  • Ensure the loop will eventually make the condition false.

Key Characteristics (AO2)

  • Condition is a pre‑condition – evaluated before the first iteration.
  • Zero or more executions are possible.
  • Best when the exact number of repetitions is unknown and may be zero.
  • Requires careful handling of the loop‑control variable to avoid infinite loops.

Example: Sum of Positive Numbers (AO1 + AO2)

Read a sequence of integers until a negative number is entered, then output the sum of the non‑negative numbers.

DECLARE sum, number AS INTEGER

SET sum ← 0

READ number

WHILE number ≥ 0 DO

SET sum ← sum + number

READ number

END WHILE

OUTPUT "Sum =", sum

Step‑by‑Step Execution

StepConditionActionSum
1number ≥ 0Enter loop (first read already performed)0
2number ≥ 0Add number to sumUpdated
3Read next numberRe‑evaluate condition
nnumber < 0Exit loopFinal sum

Edge Cases & Common Pitfalls (AO3)

  1. Infinite Loop: Forgetting to update the loop‑control variable (e.g., not reading a new number) leaves the condition true forever.
  2. Wrong Relational Operator: Using > instead of may skip the boundary value you intended to process.
  3. Zero‑Iteration Situation: If the initial condition is false the body never runs – acceptable for a WHILE loop but must be considered in design.
  4. Empty or Invalid Input: Validate input type before using it in the condition; otherwise the algorithm may terminate unexpectedly or raise a runtime error.
  5. Assuming At Least One Execution: That assumption belongs to a post‑condition loop (REPEAT … UNTIL), not a WHILE loop.

Comparison with Other Loop Types (AO2)

Loop TypeWhen Condition Is TestedGuaranteed Execution?Typical Use‑Case
Pre‑condition (WHILE … DO … END WHILE)Before each iterationNo (zero iterations possible)Repetitions unknown; may be zero.
Post‑condition (REPEAT … UNTIL)After each iterationYes (at least once)Loop must run at least once (e.g., menu selection).
Counted (FOR)Based on a counter variableDepends on range (often ≥1)Exact number of repetitions known in advance.

Mini‑Case Study – Choosing the Right Loop (AO2)

Problem: A program must read an unknown‑length text file line by line and display each line until the end of the file is reached. The file may be empty.

  • Option A – WHILE loop: WHILE NOT endoffile DO … END WHILE
  • Option B – REPEAT‑UNTIL loop: REPEAT … UNTIL endoffile
  • Option C – FOR loop: Not suitable because the number of lines is not known beforehand.

Analysis (expected answer):

  • The file could be empty, so the loop must be able to execute zero times. WHILE satisfies this because the condition is checked before the first iteration.
  • A REPEAT‑UNTIL loop would execute the body once even when the file is empty, causing an attempt to read a non‑existent line – an error.
  • A FOR loop requires a known count, which we do not have.

Best choice: WHILE loop.

Practice Exercise (AO2)

Write pseudocode for a program that repeatedly asks the user for a password until the correct password "Cambridge" is entered. Use a pre‑condition loop.

Solution Sketch

DECLARE input AS STRING

READ input

WHILE input ≠ "Cambridge" DO

OUTPUT "Incorrect, try again."

READ input

END WHILE

OUTPUT "Access granted."

Practical Worksheet – Paper 4 Preparation (AO3)

  1. Choose two allowed languages (e.g., Java and Python).
  2. Implement the “Password‑check” WHILE loop in each language.
  3. Create a simple test plan:

    • Test 1 – Correct password on first attempt.
    • Test 2 – Incorrect password entered three times before success.
    • Test 3 – Empty string entered (validate handling).

  4. Run the test cases, record the actual output, and note any errors.
  5. Write a brief evaluation (≈80 words) covering:

    • How the loop guarantees termination.
    • Potential improvements (e.g., limiting attempts, case‑insensitive comparison, input validation).

Summary (AO1 + AO2 + AO3)

  • A pre‑condition loop evaluates its condition before each iteration; the body may execute zero times.
  • Cambridge syntax: WHILE … DO … END WHILE with mandatory uppercase keywords and precise punctuation (see cheat‑sheet).
  • Use WHILE when the number of repetitions is unknown and could be zero.
  • Always update the loop‑control variable and ensure the condition will eventually become false to avoid infinite loops.
  • Combine pseudocode practice with real‑code implementation, testing and evaluation to satisfy AO1‑AO3 for the 9618 exam.

Suggested diagram: Flowchart showing the WHILE decision node, loop body, and back‑edge to the condition.