Iteration (Loops)

Test yourself on Iteration (Loops) with exam style questions, marked instantly.

Flashcards that come back just before you would forget them.

A revision plan that remembers what you know and shows what to do next.

See which topics you have nailed and which need another look.

Quiz yourself on Iteration (Loops)Get your flashcardsBuild my revision planTrack my progress

Repeating Actions

Iteration means repetition. Imagine writing "I will not be late" 100 times - boring! A loop does it for you. FOR loops repeat a set number of times (write it 100 times). WHILE loops repeat while a condition is true (keep writing until the teacher says stop). An UNTIL loop (REPEAT UNTIL on AQA, DO UNTIL on OCR) always runs at least once before checking. Loops are the power tools of programming!

Three loops drawn as flowcharts, one above the other. FOR: i is set to 1 and i <= 5 is tested. YES prints i, adds 1 to i and returns to the test. NO ends. WHILE: score < 10 is tested first. YES runs score = score + 1 and returns to the test. NO ends, so the body can run zero times. UNTIL: the body PRINT "try again" runs first, then answer = yes is tested. NO returns to the body. YES ends, so the body runs at least once.

FOR repeats a counted number of times. WHILE tests before the body, so it runs zero or more times. UNTIL tests after the body, so it runs at least once. The loop that tests at the end is REPEAT UNTIL on AQA and DO UNTIL on OCR. Here are the three loops in OCR style. The UNTIL loop adds an input line so that answer can change.

for i = 1 to 5
    print(i)
next i

score = 0
while score < 10
    score = score + 1
endwhile

do
    print("try again")
    answer = input("Again?")
until answer == "yes"

AQA style: FOR i ← 1 TO 5 ... ENDFOR, WHILE ... ENDWHILE and REPEAT ... UNTIL .... Edexcel does not set a strict pseudocode style.

Loop Types

Loop Type Use When Checks Condition
FOR Know how many times to repeat Count-controlled
WHILE Repeat while condition is true (runs zero or more times) Before each iteration
UNTIL (REPEAT UNTIL on AQA, DO UNTIL on OCR) Always run at least once After each iteration

Deep Dive: Loop Examples

FOR loop (count-controlled):

for i = 1 to 5
    OUTPUT i
next i
// Output: 1 2 3 4 5

WHILE loop (condition-controlled):

password = ""
while password != "secret"
    INPUT password
endwhile

DO UNTIL loop in OCR style (runs at least once; AQA writes REPEAT ... UNTIL):

do
    INPUT choice
until choice >= 1 AND choice <= 3

Practical Examples (GCSE Pseudocode & Python)

Example 1: Sum Numbers 1 to 100

GCSE Pseudocode:

total = 0
for i = 1 to 100
    total = total + i
next i
OUTPUT "Sum: " + total
// Output: Sum: 5050

Python Equivalent:

total = 0
for i in range(1, 101):
    total = total + i
print("Sum:", total)
# Output: Sum: 5050

Example 2: Guessing Game

secretNumber = 42
guess = 0
attempts = 0

while guess != secretNumber
    INPUT guess
    attempts = attempts + 1
    if guess < secretNumber then
        OUTPUT "Too low!"
    elseif guess > secretNumber then
        OUTPUT "Too high!"
    endif
endwhile

OUTPUT "Correct! You took " + attempts + " attempts"

Example 3: Input Validation with DO UNTIL (OCR style)

do
    INPUT age
until age >= 0 AND age <= 120
OUTPUT "Valid age entered: " + age

Memory Trick

FOR = FOR a certain number of times
WHILE = WHILE something is true, keep going
UNTIL = DO it first, check UNTIL after

Exam Tips

  • FOR vs WHILE: FOR when you know iterations, WHILE when you don't
  • Infinite loops: WHILE condition must eventually become false!
  • Common mistake: Off-by-one errors (1 to 10 vs 0 to 9)

Make this stick

Signing in gives access to all PrepWise features.

PrepWise is a trading name of A3M Quality Solutions Limited, registered in England and Wales, company number 14959172. Registered office: 202 Stroud Road, Gloucester, GL1 5LA. ICO registration ZC267069.