You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Iteration means repeating a block of code. Loops are one of the three fundamental programming constructs and are tested extensively in GCSE Computer Science (AQA 3.2 / OCR J277 2.2). This lesson covers the three types of loop you need to know.
Without loops, repeating an action 100 times would require writing the same code 100 times. Iteration lets you write the code once and repeat it as many times as needed — making programs shorter, more efficient, and easier to maintain.
A FOR loop repeats a block of code a known number of times. You use it when you know in advance how many repetitions are needed.
FOR i ← 1 TO 10
OUTPUT i
ENDFOR
This outputs the numbers 1 through 10.
for i in range(1, 11):
print(i)
Important: In Python,
range(1, 11)generates numbers from 1 to 10 (the upper bound is exclusive). In AQA pseudocode,FOR i ← 1 TO 10is inclusive of 10.
FOR i ← 0 TO 20 STEP 5
OUTPUT i
ENDFOR
Output: 0, 5, 10, 15, 20
Python equivalent:
for i in range(0, 21, 5):
print(i)
for i in range(10, 0, -1):
print(i)
This counts from 10 down to 1.
A WHILE loop repeats a block of code as long as a condition remains True. You use it when you do not know in advance how many repetitions are needed.
password ← ""
WHILE password ≠ "secret123"
OUTPUT "Enter password:"
password ← USERINPUT
ENDWHILE
OUTPUT "Access granted"
password = ""
while password != "secret123":
password = input("Enter password: ")
print("Access granted")
False at the start, the loop body never executes.False, otherwise you create an infinite loop.Exam Tip: An infinite loop is where the condition never becomes False. This is almost always a bug. Always check that something inside the loop changes the condition variable.
A DO...UNTIL loop (also called a REPEAT...UNTIL loop) checks the condition after each iteration, guaranteeing the loop body runs at least once.
REPEAT
OUTPUT "Enter a number between 1 and 10:"
number ← INT(USERINPUT)
UNTIL number >= 1 AND number <= 10
Python does not have a built-in do-while loop, but you can simulate one:
while True:
number = int(input("Enter a number between 1 and 10: "))
if 1 <= number <= 10:
break
flowchart TD
A[Start loop] --> B[Initialise counter or condition variable]
B --> C{"Check condition<br/>WHILE/FOR"}
C -->|False| D[Exit loop]
C -->|True| E[Execute loop body]
E --> F[Update counter/variable]
F --> C
D --> G[Continue with next statement]
| Feature | FOR | WHILE | DO...UNTIL |
|---|---|---|---|
| When to use | Known number of iterations | Unknown iterations, may be zero | Unknown iterations, at least one |
| Condition check | Automatic (counter-based) | Before each iteration | After each iteration |
| Minimum executions | The specified count (could be 0) | 0 (may never execute) | 1 (always runs at least once) |
| Risk of infinite loop | Very low | Yes, if condition never becomes False | Yes, if condition never becomes True |
Exam Tip: If a question says "repeat until a valid input is entered," consider a DO...UNTIL loop because the user must enter at least one value before it can be checked.
Loops can be placed inside other loops. This is called nesting and is useful for working with 2D structures (such as grids or tables).
for row in range(1, 4):
for col in range(1, 4):
print(row * col, end="\t")
print()
Output:
1 2 3
2 4 6
3 6 9
The outer loop runs 3 times. For each outer iteration, the inner loop also runs 3 times, giving 3 x 3 = 9 iterations in total.
A common pattern is to use a loop to accumulate a total or count items:
total = 0
count = 0
for i in range(5):
mark = int(input("Enter a mark: "))
total = total + mark
count = count + 1
average = total / count
print(f"Average: {average}")
n = int(input("Enter N: "))
total = 0
for i in range(1, n + 1):
total = total + i
print(f"Sum of 1..{n} = {total}")
Trace for n = 5:
| Iteration | i | total before | total after |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
| 2 | 2 | 1 | 3 |
| 3 | 3 | 3 | 6 |
| 4 | 4 | 6 | 10 |
| 5 | 5 | 10 | 15 |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.