Guide 3 of 7

Loops & Iteration

Iteration is a core concept in Computer Science. Learn the exact syntax for Definite and Indefinite loops to ace your AQA, OCR, or Cambridge Paper 2 algorithms.

⚡ 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.

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.

FOR Loop: Use when you know the exact number of iterations in advance (e.g., looping through an array of exactly 50 students).
WHILE Loop: Use when you don't know how many times to loop, and the loop might not need to run at all (e.g., checking if a file is empty before reading it).
REPEAT...UNTIL Loop: Use when the code must execute at least once before checking the condition (e.g., displaying a menu system or validating user input).

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.

FOR Loop Syntax (Cambridge)
// 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.

WHILE Loop Syntax (Cambridge)
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.

REPEAT UNTIL Syntax (Cambridge)
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!

Tap to Reveal Answer
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