Guide 3 of 7

Loops & Iteration

Iteration is a core concept in Cambridge IGCSE and A-Level Computer Science. Learn the exact syntax for FOR, WHILE, and REPEAT loops to ace your Paper 2 algorithms.

Exam Strategy: Choosing the Right Loop

One of the most common questions in the IGCSE 0478 and O-Level 2210 exams asks you to justify why you chose a specific loop structure. Examiners want to see that you understand the difference between Count-Controlled and 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).

Python vs. Pseudocode Iteration

If you code in Python, loops in pseudocode will feel very strange. Python uses indentation to control flow, but Cambridge pseudocode strictly relies on closing tags like NEXT, ENDWHILE, and UNTIL.

Loop Type 🐍 Python (Wrong for Exams) 📘 Cambridge Pseudocode (Correct)
FOR Loop for i in range(1, 11):
  print(i)
FOR i ← 1 TO 10
  OUTPUT i
NEXT i
WHILE Loop while count < 10:
  count += 1
WHILE Count < 10
  Count ← Count + 1
ENDWHILE
REPEAT Loop while True:
  x = input()
  if x == 'Y': break
REPEAT
  INPUT X
UNTIL X = 'Y'

1. The FOR Loop (Count-Controlled)

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. You must always close it with NEXT.

FOR Loop Syntax
// Outputs the numbers 1 through 10
DECLARE i : INTEGER

FOR i ← 1 TO 10
    OUTPUT "Current number is: ", i
NEXT i

2. The WHILE Loop (Pre-Condition)

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
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. The REPEAT...UNTIL Loop (Post-Condition)

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
DECLARE Guess : INTEGER

REPEAT
    OUTPUT "Guess a number between 1 and 5:"
    INPUT Guess
UNTIL Guess = 3

OUTPUT "You guessed it!"

Common Syllabus Mistakes

When marking Paper 2 exams, Cambridge examiners frequently dock points for these two common iteration mistakes:

❌ Forgetting to increment in a WHILE loop: A FOR loop increments automatically. A WHILE loop does not. If you forget to update your counter inside a WHILE loop, you create an infinite loop and lose marks.

❌ Forgetting the NEXT statement: It is not enough to just indent your code. You must explicitly end your FOR loops with NEXT i. Without it, the pseudo-compiler will throw a syntax error.

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