🔒

Unlock Pro

Subscribe to access all 59 Edexcel 1CP2 lessons.

£7.99/month
or £59/year
🐍 Paper 2 · Topic 6: Programming
6.1d Iteration: for & while Loops
Edexcel 1CP2 · GCSE Computer Science · ~10 min read · 🔒 Pro
Notes
──
Video
──
Slides
──
Worksheet
──
Quiz

What is Iteration?

Iteration means repeating a block of code. Instead of writing the same code multiple times, we use a loop to execute it repeatedly. Python has two main loop types: for loops (count-controlled) and while loops (condition-controlled).

for Loops — Count-Controlled Iteration

A for loop repeats a set number of times. It iterates over a sequence (like a range of numbers or items in a list).

# Print 1 to 5 using range() for i in range(1, 6): print(i) # Output: 1 2 3 4 5

range() function:

SyntaxWhat it producesExample
range(n)0, 1, 2, ... n-1range(5) → 0, 1, 2, 3, 4
range(start, stop)start, start+1, ... stop-1range(1, 6) → 1, 2, 3, 4, 5
range(start, stop, step)start, start+step, ... up to stoprange(0, 10, 2) → 0, 2, 4, 6, 8
# Iterating over a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Output: apple banana cherry # Summing numbers with a for loop total = 0 for i in range(1, 6): total += i print(total) # Output: 15

while Loops — Condition-Controlled Iteration

A while loop repeats as long as a condition remains True. The number of iterations is not fixed — it depends on when the condition becomes False.

# Basic while loop count = 1 while count <= 5: print(count) count += 1 # Output: 1 2 3 4 5 # Input validation with while loop password = input("Enter password: ") while password != "secret": print("Wrong password, try again") password = input("Enter password: ") print("Access granted!")

for vs while — When to Use Each

for loopwhile loop
Use when you know how many times to repeatUse when you repeat until a condition is met
Iterating over a list or rangeInput validation (repeat until valid input)
Processing each element of a collectionGame loops (repeat until player loses)
Counting (1 to 10)Searching (repeat until found)
Always terminates (given finite range/list)Risk of infinite loop if condition never becomes False

Nested Loops

A loop inside another loop is called a nested loop. The inner loop completes all its iterations for every single iteration of the outer loop.

# Multiplication table using nested loops for i in range(1, 4): for j in range(1, 4): print(i * j, end=" ") print() # Output: # 1 2 3 # 2 4 6 # 3 6 9

break and continue

KeywordEffectExample
breakImmediately exits the loop entirelySearch a list and stop when item is found
continueSkips the rest of the current iteration and goes to the nextSkip even numbers, only process odd
# break example — stop at 5 for i in range(1, 10): if i == 5: break print(i) # Output: 1 2 3 4 # continue example — skip even numbers for i in range(1, 8): if i % 2 == 0: continue print(i) # Output: 1 3 5 7
Exam tip: Paper 2 often asks you to trace loops (show the value of variables after each iteration). Always track the loop variable AND any accumulators. Remember range(1,6) gives 1,2,3,4,5 — NOT 6. The stop value is excluded.
⚠️ Common Mistakes
  • Infinite while loop — forgetting to update the variable inside the loop so the condition never becomes False
  • Off-by-one errors — range(1, 10) produces 1–9, not 1–10. To include 10, use range(1, 11)
  • Wrong indentation — Python uses indentation to define loop bodies; inconsistent indentation causes errors
  • Modifying a list while iterating over it — can cause unexpected behaviour
Video coming soon
Click slide or press arrow keys to navigate

✍️ Worksheet — 6.1d Iteration: for & while Loops

8 questions

Q1What is the difference between a for loop and a while loop in Python? When would you use each?[4]
✅ Mark scheme
A for loop is count-controlled — it repeats a fixed number of times, iterating over a sequence or range [1]; use a for loop when you know in advance how many times to repeat, e.g. processing each item in a list [1]; a while loop is condition-controlled — it repeats as long as a condition is True; the number of iterations is not predetermined [1]; use a while loop when repeating until a condition is met, e.g. input validation, game loops, or searching [1].
Q2What values does range(2, 10, 3) produce? Explain each part of the range() call.[4]
✅ Mark scheme
range(2, 10, 3) produces: 2, 5, 8 [1]; start=2 — the sequence begins at 2 [1]; stop=10 — the sequence goes up to but NOT including 10 [1]; step=3 — each value increases by 3 (so 2 → 5 → 8; next would be 11 which exceeds 10, so it stops) [1].
Q3Trace the following code. Show the value of total after each iteration of the loop.

total = 0
for i in range(1, 5):
    total = total + i
print(total)
[4]
✅ Mark scheme
i=1: total = 0+1 = 1 [1]; i=2: total = 1+2 = 3 [1]; i=3: total = 3+3 = 6 [1]; i=4: total = 6+4 = 10 [1]; print(total) outputs: 10. (range(1,5) gives 1,2,3,4 — NOT 5.)
Q4Write Python code using a while loop that asks the user to enter a number between 1 and 10 and keeps asking until a valid number is entered.[4]
✅ Mark scheme
num = int(input("Enter a number (1-10): "))
while num < 1 or num > 10:
    print("Invalid! Try again.")
    num = int(input("Enter a number (1-10): "))
print("Valid number:", num)
Mark: getting input before the loop [1]; while condition checks both boundaries (num<1 or num>10) [1]; print error message inside loop [1]; get input again inside loop (essential — otherwise infinite loop) [1].
Q5Explain what an infinite loop is and give an example of how one could occur in Python with a while loop.[3]
✅ Mark scheme
An infinite loop is a loop that never terminates — the condition controlling it never becomes False [1]; example: forgetting to update the loop variable inside a while loop [1]; e.g. count = 1; while count < 10: print(count) — count is never incremented so the condition (count < 10) is always True and the loop runs forever [1].
Q6What does the break keyword do in a loop? Write a short Python example using break.[3]
✅ Mark scheme
break immediately terminates the current loop, regardless of whether the loop condition is still True or there are more iterations remaining [1]; example: for i in range(1, 10): [1] if i == 5: break; print(i) — this prints 1, 2, 3, 4 and then exits the loop when i reaches 5 [1].
Q7Write Python code using a for loop to print the multiplication table for 7 (7×1 to 7×10).[4]
✅ Mark scheme
for i in range(1, 11):
    print("7 x", i, "=", 7 * i)
Mark: correct for loop syntax with range [1]; range(1, 11) to include 10 [1]; correct multiplication 7*i [1]; print statement showing the multiplication [1]. Output: 7 x 1 = 7, 7 x 2 = 14, ..., 7 x 10 = 70.
Q8Describe and explain the output of the following nested loop code:

for i in range(3):
    for j in range(3):
        print(i, j)
[4]
✅ Mark scheme
The outer loop runs for i = 0, 1, 2 (three times) [1]; for each value of i, the inner loop runs completely for j = 0, 1, 2 (three times) [1]; total of 9 outputs [1]; output: 0 0 / 0 1 / 0 2 / 1 0 / 1 1 / 1 2 / 2 0 / 2 1 / 2 2 [1]. (Accept trace table format showing all pairs.)
Topic Quiz
Q 1 of 15
You scored
out of 15
Click to reveal definition
🎉
Session complete!
TermDefinition
🎯

Mini Test — Iteration

10 minutes · Exam-style

← 6.1c SubroutinesTopic 6 · Programming6.2a Functions →