⚡ 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.
What keyword is used in pseudocode to represent a loop with a condition at its beginning?
Answer: WHILE. A WHILE loop is a pre-condition (or top-tested) loop that tests its condition before executing any code. If the condition evaluates to FALSE on entry, the loop body executes zero times. In contrast, REPEAT...UNTIL is a post-condition loop tested at the end.
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 DO ... ENDWHILE | REPEAT ... UNTIL X = 10 |
| AQA | FOR i ← 1 TO 10 ... ENDFOR | WHILE X < 10 DO ... ENDWHILE | REPEAT ... UNTIL X = 10 |
| OCR | for i = 1 to 10 ... next i | while x < 10 do ... 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 iWhat keyword in structured English or pseudocode do we use to finish a count-controlled loop?
Answer: NEXT (or ENDFOR). In Cambridge IGCSE and A-Level pseudocode, a count-controlled FOR loop is finished with the NEXT keyword followed by the counter variable (e.g., NEXT i). In AQA structured English, ENDFOR is also accepted. The NEXT statement increments the counter variable and evaluates whether the loop boundary has been reached.
Using STEP to Count Backwards
By default, a FOR loop increments its counter by 1. If you need to count backwards or increment by a different number, you must use the STEP keyword. For example, to loop from 8 down to 1, you must use STEP -1.
// Outputs the numbers from 8 down to 1
DECLARE count : INTEGER
FOR count ← 8 TO 1 STEP -1
OUTPUT count
NEXT count2. 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" DO
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!"4. Pre-Condition vs Post-Condition Loops
A frequent Cambridge IGCSE (0478), O-Level (2210), and A-Level (9618) exam question asks you to compare a pre-condition loop (WHILE ... DO ... ENDWHILE) with a post-condition loop (REPEAT ... UNTIL):
- Pre-condition loop (
WHILE): Tests the condition at the beginning before entering the loop body. If the condition is initiallyFALSE, the body executes zero times. It continues looping while the condition remainsTRUE. - Post-condition loop (
REPEAT ... UNTIL): Tests the condition at the end after running the loop body. Therefore, the statements inside always execute at least once, and the loop stops as soon as the condition becomesTRUE. - Count-controlled loop (
FOR ... TO ... NEXT): Used for definite iteration when the exact number of repetitions is known before the loop starts. Finish a count-controlled loop with the keywordNEXTfollowed by the counter variable.
5. More Pseudocode Loop Examples
Click ▶ Run this example on any snippet below to execute it in the online pseudocode compiler or dry-run it in a trace table:
DECLARE n : INTEGER
DECLARE SumEven : INTEGER
SumEven ← 0
FOR n ← 2 TO 10 STEP 2
SumEven ← SumEven + n
NEXT n
OUTPUT "Sum of even numbers (2..10): ", SumEvenDECLARE Score : INTEGER
REPEAT
OUTPUT "Enter an exam score between 0 and 100:"
INPUT Score
UNTIL Score >= 0 AND Score <= 100
OUTPUT "Valid score accepted: ", ScoreInteractive 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
Need to Find the Highest, Lowest, or Count Occurrences in a Loop?
Learn standard Cambridge patterns for loop accumulators, rogue values, and finding minimum and maximum values.
📝 Ready to Practice Loops?
Test your loop skills with interactive exam-style questions, dry-run trace tables, and automated grading.
