Repeat while a condition is true
Use a pre-condition loop when the body should run only while a condition holds.
Section 8 · Programming
Exam scope and assessment notes
This sequence follows Cambridge IGCSE Computer Science 0478, 2026–2028 syllabus Version 6. It covers section 8.1 (Programming concepts) only. Pseudocode examples use the published Cambridge format; the separate 2029–2031 syllabus is not mapped here. Practice is original, self-checked and does not award an official Cambridge grade. Full timed papers, a browser-based programming workspace and human subject-teacher review remain outstanding.
Before you begin
- Be comfortable following an ordered algorithm and tracing variable values. Each lesson introduces the Cambridge pseudocode form it uses.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
When is the condition of a WHILE loop checked?
What you will learn
- Trace a WHILE loop condition
- Update values so the loop can end
- Handle a condition that is false at the start
WHILE checks before each iteration
A pre-condition loop tests its condition before entering the body. If the condition is initially FALSE, the body runs zero times.
Change a value in the body so that a valid loop can eventually stop. Otherwise the condition may remain TRUE forever.
Choose a condition that matches the stop rule
WHILE attempts < 3 repeats as long as fewer than three attempts have been made. The final iteration should update Attempts so the next condition check can fail.
Use a pre-condition loop when zero repetitions is valid, such as reading while more data remains.
Pause and explain
What should a WHILE body usually do to a loop-control value?
Worked example
Start Count at 1 and output it while it is less than 4.
Show the worked solution
- Set Count ← 1.
- Check Count < 4 before each pass.
- Output Count and increment it, so the values are 1, 2 and 3.
Answer Count ← 1 WHILE Count < 4 DO OUTPUT Count Count ← Count + 1 ENDWHILE
Try a topic test
Choose Easy, Normal or Difficult. Save your answers and open worked review after finishing.
Test this topic →From guided practice to a new situation
Write your answer and reasoning first. Use a hint only if you are stuck. The model solution is for self-checking; your written work is not automatically marked.
Trace Count ← 2; WHILE Count <= 4: OUTPUT Count; Count ← Count + 1. Give the output sequence.
Give me a hint
Check the condition before each pass and update the count.
Compare my reasoning
- Count 2 passes and becomes 3.
- Count 3 passes and becomes 4.
- Count 4 passes and becomes 5; 5 <= 4 is false.
The output is 2, 3, 4.
Look for these in your work
- Condition traced
- Update traced
- Output sequence
Write a loop that processes scores while Count is less than 5, incrementing Count after each score.
Give me a hint
Place the increment inside the repeated body.
Compare my reasoning
- Test Count < 5 before processing.
- Process one score.
- Increment Count before the next condition check.
WHILE Count < 5 DO ProcessScore Count ← Count + 1 ENDWHILE
Look for these in your work
- Pre-condition
- Progress update
A WHILE loop reads another command while Command = "Y". Explain how to avoid repeating the same command forever.
Give me a hint
The loop body must obtain the next command.
Compare my reasoning
- Process the current command.
- Prompt and read a new Command inside the loop.
- The next condition check uses that new input.
Read a new Command in each iteration; stop when it is no longer Y.
Look for these in your work
- New input inside loop
- Stopping condition explained
Common mistakes
- Forgetting the update and creating an infinite loop
- Using a post-condition loop when zero runs are required
- Changing the condition so it can never become false
What can you explain now?
Can a WHILE body run zero times?
Compare with the explanation
Yes, if its initial condition is false
The condition is tested before the first iteration.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Make it stick
What must happen inside a WHILE loop for it to make progress?
If you needed the solution, close it and solve the problem again from a blank page. A correct answer is stronger when you can explain the reason for each step.