⚡ Quick Review: Loop Types
- FOR Loop (Count-Controlled / Definite): Used when the exact number of iterations is known before the loop starts.
- WHILE Loop (Pre-Condition / Indefinite): Checks the condition at the start. The code inside might never execute if the condition is false initially.
- REPEAT UNTIL Loop (Post-Condition / Indefinite): Checks the condition at the end. Guarantees the code inside will run at least once.
In this guide:
Exam Strategy: Choosing the Right Loop
One of the most common questions in Computer Science exams asks you to justify why you chose a specific loop structure. Examiners want to see that you understand the difference between Definite (Count-Controlled) and Indefinite (Condition-Controlled) iteration.
AQA vs OCR vs Cambridge Syntax
While the logic of iteration is universal, how you write them changes depending on your exam board. Cambridge (CIE 0478 & 9618) strictly uses uppercase, while AQA and OCR have different closing keywords.
| Exam Board | Count-Controlled (FOR) | Pre-Condition (WHILE) | Post-Condition (REPEAT) |
|---|---|---|---|
| Cambridge (CIE) | FOR i ← 1 TO 10 ... NEXT i |
WHILE X < 10 ... ENDWHILE |
REPEAT ... UNTIL X = 10 |
| AQA | FOR i ← 1 TO 10 ... ENDFOR |
WHILE X < 10 ... ENDWHILE |
REPEAT ... UNTIL X = 10 |
| OCR | for i = 1 to 10 ... next i |
while x < 10 ... endwhile |
do ... until x == 10 |
1. Definite Iteration: The FOR Loop
The FOR loop is count-controlled. It uses a counter variable (usually i or count) that automatically increments every time the loop completes a cycle. In Cambridge syntax, you must always close it with NEXT.
// Outputs the numbers 1 through 10
DECLARE i : INTEGER
FOR i ← 1 TO 10
OUTPUT "Current number is: ", i
NEXT i
2. Indefinite Iteration: The WHILE Loop
The WHILE loop evaluates its condition before running the code block. This means if the condition is false initially, the code inside the loop is completely ignored. This is known as pre-condition iteration.
DECLARE Password : STRING
Password ← ""
// Keeps asking until the user types "secret"
WHILE Password <> "secret"
OUTPUT "Enter the password:"
INPUT Password
ENDWHILE
OUTPUT "Access Granted!"
3. Indefinite Iteration: The REPEAT...UNTIL Loop
Unlike the WHILE loop, the REPEAT...UNTIL loop checks its condition at the bottom of the block. Therefore, the algorithm guarantees the code will run a minimum of one time. This makes it the perfect loop for validating user inputs.
DECLARE Guess : INTEGER
REPEAT
OUTPUT "Guess a number between 1 and 5:"
INPUT Guess
UNTIL Guess = 3
OUTPUT "You guessed it!"
Interactive Exam Practice
Scenario: Write an algorithm that takes exactly 5 numbers as input from the user. It should add these numbers together to find a running total, and then output the final total sum at the end.
Hover or tap the black box to reveal the examiner's solution. Click Run Code to test it instantly!
DECLARE Total : INTEGER DECLARE Num : INTEGER DECLARE i : INTEGER // Must initialize total to 0! Total ← 0 FOR i ← 1 TO 5 OUTPUT "Enter a number:" INPUT Num Total ← Total + Num NEXT i OUTPUT "The total sum is: ", Total