Community resourceWorksheet

J277 2.2.1 Condition-controlled iteration

Part 6 of 7 · J277 2.2.1-2.2.2 · Programming fundamentals

Condition-controlled iteration, for when the number of repetitions is not known in advance.

Students will:

  • explain when a while loop is the right choice
  • write a loop whose condition can become false
  • avoid loops that never finish
  • trace the values the condition depends on
  • repeat until a threshold is reached

Inside: 6 explanation cells, 3 runnable Python tasks, 4 multiple-choice questions and 2 written answers. 15 marks, about 45 minutes.

Series: J277 2.2.1-2.2.2 · Programming fundamentals, part 6 of 7.

Shared by Coding PathwayVerified teacher

  • 15 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 17 Aug 2026
  • Updated 9 Sept 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Condition-controlled iteration

A condition-controlled loop is suitable when the number of repetitions is not known in advance. Python's while loop repeats while its condition is true. The values used by the condition must be able to change, or the loop may never finish.

Multiple choice1 mark

A program keeps asking for a password until the correct password is entered. Which construct is most suitable?

  • AA condition-controlled loop
  • BA single assignment
  • CA count-controlled loop with exactly two repeats
  • DA constant

A reliable while loop

  1. Initialise the value tested by the condition.
  2. Test the condition using while.
  3. Perform the repeated work.
  4. Update or re-input the value so the condition can eventually become false.
The control flow of a reliable while loop Initialise a control value, test the while condition, run the loop body when true, update the control value and return to the test. When the condition is false, leave the loop. Initialise before the loop condition true? True Run loop body Update or re-input then test again False Leave the loop

The update arrow returns to the condition. If the tested value never changes, the loop may follow the True path forever.

For input validation, the loop condition normally describes an invalid value: while age < 11 or age > 18:. Repetition stops when a valid value is entered.

Before running the example, predict how many times "Move" will be displayed and the final value of energy. Use the update statement to explain why the loop terminates.

Worked example
energy = 3
while energy > 0:
    print("Move")
    energy = energy - 1
print("Rest")

Reading OCR's do until loop

OCR Exam Reference Language may also use a condition-controlled do ... until loop:

do
    answer = input("Enter Y")
until answer == "Y"

The statements run before the condition is checked, so a do until loop always runs at least once. It repeats until its condition becomes true. A Python while loop normally checks its condition before the first repetition.

Multiple choice1 mark

Why does an OCR Exam Reference Language do ... until loop always run its body at least once?

  • AIts condition can never be true
  • BThe condition is checked after the body
  • CIt is always count-controlled
  • DPython automatically calls it
Multiple choice1 mark

Why does the worked loop finish?

  • Aprint() automatically stops every loop
  • Bwhile loops always run three times
  • Cenergy decreases until `energy > 0` is false
  • Denergy is a constant

Supported practice: a countdown

Use a while loop to record a countdown from 5 to 1 in the list values. The starting value and empty list are supplied.

values = [] creates an empty Python list, and values.append(number) adds the current number to its end. The list lets the checks inspect the complete countdown.

Your condition must allow 1 to be processed. Inside the loop, append the current number and then decrease it so the condition eventually becomes false. The final list should be [5, 4, 3, 2, 1].

Coding task3 marks
# Count down from 5 to 1 using a while loop.
number = 5
values = []
# Write the loop here.

Independent practice: repeat to a threshold

Begin with value = 3. Repeatedly double value until it is at least 100, and use steps to count how many doublings occur.

The loop should continue only while the current value is below the target. Both variables must be updated inside the loop so the program reaches the target and terminates.

Coding task3 marks
# Keep doubling value until it is at least 100.
value = 3
steps = 0
# Write the while loop here.
Written answer3 marks

Find and correct the problem in this loop:

lives = 3
while lives > 0:
    print(lives)

Explain what happens, then state the update that is needed inside the loop.

Students type their answer here.

Written answer2 marks

A student says that while answer != "yes": repeats when the answer is yes. Correct the misunderstanding.

State when the condition is true and when repetition stops.

Students type their answer here.

Multiple choice1 mark

Which condition repeats while choice is neither A nor B?

  • Achoice != 'A' or choice != 'B'
  • Bchoice == 'A' and choice == 'B'
  • Cnot choice
  • Dchoice != 'A' and choice != 'B'

Review

A Python while loop repeats while its condition is true. Before running one, identify the initial value, the continuation condition and the update that can eventually make the condition false. If any of these is missing, trace a few repetitions before risking an infinite loop.