Iteration: Condition-Controlled
Repeat while a Boolean condition remains True.
A condition-controlled loop is used when the number of repetitions is not known in advance. Python uses while.
Predict
Read the code first. Do not run it yet.
number = 1
while number < 10:
number = number * 2
print(number)- List the value of
numberafter each repetition. - How many times does the loop run?
- Why does the loop stop?
Investigate
Use this code to explain exactly how the program works.
password = ""
while password != "python":
password = input("Password: ")
print("Accepted")- Which condition controls repetition?
- What must become false for the loop to finish?
- Why might this loop run once, many times, or never finish?
A while loop
count = 3
while count > 0:
print(count)
count = count - 1
print("Go")3 2 1 Go
- The condition is checked before each repetition.
- The loop body changes
count. - When
count > 0becomes False, execution continues after the loop.
Self-study guide
Condition-controlled iteration repeats while a condition says repetition should continue. The number of repetitions is not fixed in advance. In Python this course uses while, which checks its condition before each repetition.
OCR Exam Reference Language includes both a pre-test WHILE loop and a post-test DO UNTIL loop. Python has a direct while statement but no direct do until keyword. The central GCSE idea is choosing a condition that controls when repetition stops.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
while condition ... endwhile | while condition: | Pre-test condition-controlled loop. |
do ... until condition | no direct Python keyword | OCR ERL includes a post-test DO UNTIL. Python normally expresses the required logic with a suitable while structure. |
while answer != "Correct" | while answer != "Correct": | The continuation condition is directly comparable. |
Worked example
Validation often uses a while loop because you do not know how many invalid attempts a user will make.
rating = int(input("Rating 1-5: "))
while rating < 1 or rating > 5:
rating = int(input("Rating 1-5: "))
print("Accepted", rating)Accepted 4
- The first value is input before the loop.
- If the value is invalid, the condition is true and the loop repeats.
- The variable must be updated inside the loop; otherwise the condition could stay true forever.
- When the rating is from 1 to 5 inclusive, the condition becomes false and execution continues.
Another example
A sentinel value can tell a condition-controlled input loop when to stop.
total = 0
number = int(input("Number, -1 to stop: "))
while number != -1:
total = total + number
number = int(input("Number, -1 to stop: "))
print(total)27
-1is a sentinel: a special value that means stop.- The sentinel is checked before the loop body, so it is not added to the total.
Common mistakes
- Forgetting to change a variable used by the loop condition.
- Choosing a
forloop when the number of repetitions depends on user input. - Writing a stopping condition when Python
whileexpects a continuation condition, without reversing the logic correctly.
Exam-ready checklist
- Choose condition-controlled iteration when the number of repetitions is unknown.
- Trace a
whileloop accurately. - Explain the difference between OCR WHILE and DO UNTIL.
- Use validation and sentinel patterns correctly.
What you have learned
- Condition-controlled iteration repeats while a condition is True.
- Python uses
while. - The condition is checked before every repetition.
- A loop can become infinite if its controlling condition never becomes False.