Embedding 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)
Keyword
Purpose
Exact spelling / punctuation
DECLARE
Introduce one or more variables
DECLARE var1, var2 AS TYPE (comma‑separated, single space before AS)
SET
Assign a value
SET var ← expression (arrow symbol, spaces around it)
READ
Input from the user
READ var
OUTPUT
Display a value or message
OUTPUT "text", var (comma separates items)
WHILE … DO
Start of a pre‑condition loop
All caps, single space before and after the condition
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
Step
Condition
Action
Sum
1
number ≥ 0
Enter loop (first read already performed)
0
2
number ≥ 0
Add number to sum
Updated
3
Read next number
Re‑evaluate condition
—
…
…
…
…
n
number < 0
Exit loop
Final sum
Edge Cases & Common Pitfalls (AO3)
Infinite Loop: Forgetting to update the loop‑control variable (e.g., not reading a new number) leaves the condition true forever.
Wrong Relational Operator: Using > instead of ≥ may skip the boundary value you intended to process.
Zero‑Iteration Situation: If the initial condition is false the body never runs – acceptable for a WHILE loop but must be considered in design.
Empty or Invalid Input: Validate input type before using it in the condition; otherwise the algorithm may terminate unexpectedly or raise a runtime error.
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 Type
When Condition Is Tested
Guaranteed Execution?
Typical Use‑Case
Pre‑condition (WHILE … DO … END WHILE)
Before each iteration
No (zero iterations possible)
Repetitions unknown; may be zero.
Post‑condition (REPEAT … UNTIL)
After each iteration
Yes (at least once)
Loop must run at least once (e.g., menu selection).
Counted (FOR)
Based on a counter variable
Depends 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)
Choose two allowed languages (e.g., Java and Python).
Implement the “Password‑check” WHILE loop in each language.
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).
Run the test cases, record the actual output, and note any errors.
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.
Support e-Consult Kenya
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources,
past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.