Menu

Pseudocode

Condition-Controlled Loops: WHILE and REPEAT UNTIL

A WHILE loop is condition-controlled: it repeats a block of statements while a condition is true. The condition is checked at the start of every iteration. If it is false the first time, the body never runs.

people_waiting = int(input("How many are waiting? "))
while people_waiting > 0:
    print("Serving a customer")
    people_waiting = people_waiting - 1
print("Queue empty")

Output when the user types 0:

How many are waiting? 0
Queue empty

Here, the loop serves customers only while the number waiting is above zero. Enter 0 and the body is skipped completely, so a WHILE loop can run zero times.

Two flowcharts side by side. WHILE tests people_waiting > 0 before the body, so the body can run zero times. REPEAT UNTIL runs the body first and then tests the condition, looping back while it is false.

REPEAT UNTIL

A REPEAT UNTIL loop is another condition-controlled loop, but the condition is tested after the body. The body always runs at least once. It continues until the condition becomes true, which means it loops while the condition is false. This suits a task where you must do something once before you can decide whether to stop, such as asking for a value and checking it afterwards.

FeatureWHILEREPEAT UNTIL
Where the condition is testedAt the startAfter the body
Fewest runs of the body01
Repeats whileThe condition is trueThe condition is false
Stops whenThe condition is falseThe condition is true

Practise this