11-condition-controlled-iteration.py
python-programming / programming-constructs  ·  Lesson 12
Lesson 12 — Programming constructs

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.

condition-controlled-iteration-predict.py
number = 1
while number < 10:
    number = number * 2
print(number)
Predict before running
  • List the value of number after 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.

condition-controlled-iteration-investigate.py
password = ""
while password != "python":
    password = input("Password: ")
print("Accepted")
Investigate the code
  • 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

countdown.py
count = 3
while count > 0:
    print(count)
    count = count - 1
print("Go")
Output
3
2
1
Go
How it works
  • The condition is checked before each repetition.
  • The loop body changes count.
  • When count > 0 becomes False, execution continues after the loop.
Avoid infinite loopsSomething in the loop normally needs to change so that the condition can eventually become False.

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 focusUnderstand and use condition-controlled loops. Know that an OCR WHILE checks before entering the loop, while OCR DO UNTIL performs the body once before its condition is checked.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
while condition ... endwhilewhile condition:Pre-test condition-controlled loop.
do ... until conditionno direct Python keywordOCR 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.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Validation often uses a while loop because you do not know how many invalid attempts a user will make.

lesson-12-worked.py
rating = int(input("Rating 1-5: "))

while rating < 1 or rating > 5:
    rating = int(input("Rating 1-5: "))

print("Accepted", rating)
Output
Accepted 4
Work through it line by line
  • 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.

lesson-12-example-2.py
total = 0
number = int(input("Number, -1 to stop: "))

while number != -1:
    total = total + number
    number = int(input("Number, -1 to stop: "))

print(total)
Output
27
Why this example matters
  • -1 is 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

Avoid these errors
  • Forgetting to change a variable used by the loop condition.
  • Choosing a for loop when the number of repetitions depends on user input.
  • Writing a stopping condition when Python while expects a continuation condition, without reversing the logic correctly.
Exam-safe warningAlways ask: “What must change so this condition eventually becomes false?” This prevents accidental infinite loops.

Exam-ready checklist

You should now be able to
  • Choose condition-controlled iteration when the number of repetitions is unknown.
  • Trace a while loop 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.