All subjects · Computer Science · WCP02 Practical Programming and Problem-solving
Condition-controlled iteration: while loops
Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving
A condition-controlled loop repeats while a condition stays True. Use it when you do not know in advance how many passes are needed.
The while loop (spec 7.1.1)
Python's while loop is pre-conditioned: the condition is tested before each pass, so the body may run zero times. Something inside the loop must eventually make the condition False, or the loop never ends (an infinite loop).
n = 1
while n < 50:
n = n * 3
print(n)Output:
81Validation loops
A while loop is the standard way to keep asking until the input is acceptable:
mark = int(input("Mark (0 to 100): "))
while mark < 0 or mark > 100:
print("Out of range")
mark = int(input("Mark (0 to 100): "))If the user types 120, then -5, then 64, the message 'Out of range' is shown twice and mark ends as 64.
Loops controlled by a flag
A Boolean flag can stop a loop when something is found. This replaces a break statement, which is not in the Programming Language Subset.
items = [3, 8, 12, 5]
found = False
i = 0
while i < len(items) and not found:
if items[i] > 10:
found = True
else:
i = i + 1
print(found, i)Output:
True 2for or while?
- Fixed number of passes, or every item in a structure: for (count-controlled).
- Repeat until something happens (valid input, item found, total reached): while (condition-controlled).
Any for loop can be rewritten as a while loop with a counter that you initialise and increment yourself.
Worked example
Rewrite for i in range(2, 9, 3): print(i) as a while loop.
- Initialise the counter: i = 2
- Condition: while i < 9:
- Body: print(i), then step: i = i + 3
- Both versions display: 2 5 8
Answer: i = 2 / while i < 9: / print(i) / i = i + 3
Check yourself
- What is displayed when this code runs?
```python
total = 0
k = 0
while total < 20:
k = k + 1
total = total + k
print(k, total)
```
Show answer
6 21. total goes 1, 3, 6, 10, 15, 21. It stops when total is no longer below 20: 6 21.
- What is the value of steps after this code runs?
```python
x = 100
steps = 0
while x > 1:
x = x // 2
steps = steps + 1
```
Show answer
6. 100, 50, 25, 12, 6, 3, 1: x is halved 6 times.
- What is displayed when this code runs?
```python
count = 10
while count < 5:
count = count + 1
print(count)
```
Show answer
10. The condition 10 < 5 is False at the first test, so the body never runs and 10 is displayed.
- Why does this loop never stop?
```python
i = 0
while i < 5:
print(i)
```
- The condition should use <=
- print() cannot be used in a loop
- i is never changed inside the loop
- i should start at 1
Show answer
i is never changed inside the loop. Nothing in the body changes i, so i < 5 stays True for ever. Add i = i + 1.
- The user types 1111, then 1234, then 4321. What is displayed?
```python
attempts = 0
pin = input()
while pin != "4321":
attempts = attempts + 1
pin = input()
print(attempts)
```
Show answer
2. The first two inputs are wrong, so the loop body runs twice before the correct PIN is entered: 2.
- This code should keep doubling d until it is at least 1000, then print it. Which line completes it?
```python
d = 3
__________
d = d * 2
print(d)
```
- `while d > 1000:`
- `while d <= 100:`
- `while d < 1000:`
- `while d == 1000:`
Show answer
`while d < 1000:`. Loop while d is still below 1000; the first value of at least 1000 is then printed.
- This code should display the index of the target (2), but it does not. Which line number causes the error?
```
1 data = [5, 9, 2, 7]
2 target = 2
3 i = 0
4 found = False
5 while i < len(data) and not found:
6 if data[i] == target:
7 found = True
8 i = i + 1
9 print(i)
```
Show answer
8. Line 8 adds 1 to i even after the target is found, so i ends one past the item's index. Put i = i + 1 in an else branch.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.