- Understand what a conditional loop is and how it differs from a fixed loop
- Use
whileto repeat instructions until a condition becomes false - Avoid writing an infinite loop
- I can write a
whileloop with a working condition - I can explain why a loop must change something that affects its condition
- I can identify and fix an infinite loop
Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.
1. "Keep stirring until the sauce thickens" is an example of repeating something...
2. Which sounds like it could repeat forever if something goes wrong?
3. True or false: with a fixed loop, you always know the exact number of repeats in advance; with a conditional loop, you might not.
Key vocabulary
Conditional Loops (while)
What is a conditional loop?
while total < target:If the loop control variable never changes, the condition may stay true forever.
A conditional loop repeats a block of instructions for as long as a condition stays true, rather than a fixed, known number of times. This is the key difference from the fixed loop covered last lesson: with a fixed loop, the number of repeats is decided before the loop starts; with a conditional loop, the number of repeats depends on something that can change while the program runs, so it isn't known in advance.
The while loop
In Python, a conditional loop is written using while followed by a condition and a colon, exactly like the conditions used in selection (==, !=, <, >, <=, >=). The indented block underneath repeats over and over, and before every single repeat, Python checks the condition again. The moment the condition is false, the loop stops and the program continues with whatever comes after it.
The loop control variable must change
For a while loop to ever stop, something inside the loop must change the value that the condition is checking. If nothing inside the loop changes that value, the condition will never become false, and the loop will repeat forever — this is called an infinite loop, and it is one of the most common bugs pupils write when first learning conditional loops.
Sentinel-controlled loops
A common real use of a while loop is a sentinel-controlled loop — one that keeps asking for input until the user enters a special value (the sentinel) that means "stop." For example, a program might keep asking "Enter a number, or type 'done' to finish:" and loop until the user actually types done.
Worked examples
A program prints "Still going" while a counter is below 3.
count = 0while count < 3:print("Still going")count = count + 1count increases by 1 (0 → 1 → 2 → 3), and once count reaches 3, count < 3 is false, so the loop stops.A program that never stops.
count = 0while count < 3:print("Still going")count is never updated inside the loop, so it stays at 0 forever, and count < 3 is always true. This is exactly the bug from Example 1, just with the count = count + 1 line missing.A program keeps asking for a password until the correct one is entered.
password = input("Enter the password: ")while password != "python123":password = input("Incorrect. Try again: ")"python123". The input() call inside the loop is what updates password — without it, this would also become an infinite loop.A program contains: total = 0, then while total < 10: with total = total + 3 indented underneath, then print(total) after the loop.
Answer the following:
- What values does
totaltake across each pass of the loop? - How many times does the loop run?
- What value is printed at the end?
totalgoes 0 → 3 → 6 → 9 → 12.- The loop runs 4 times (checked before each pass: 0<10 true, 3<10 true, 6<10 true, 9<10 true, 12<10 false — stop).
12is printed — the loop stops as soon astotalreaches or passes 10, it doesn't stop exactly at 10.
= instead of == in the condition. Just like with if, a while condition needs the comparison operator ==, not the assignment operator =.If your program seems frozen and won't stop: it's very likely an infinite loop. In IDLE, press Ctrl+C in the Shell window to force it to stop, then check whether the loop control variable is actually being updated inside the loop.
Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.
1. What is the main difference between a fixed loop and a conditional loop? TYPE 1
2. x = 0 then while x < 4: print(x); x = x + 1. How many times does the loop run? TYPE 1
3. What is wrong with this code? count = 0 / while count < 5: / print(count) TYPE 1
4. A while loop's condition is checked TYPE 1
5. What is a sentinel value used for? TYPE 1
6. Explain what an infinite loop is and give one reason it might happen. TYPE 2 N3
7. Explain why the loop control variable must be updated inside the loop, not after it. Use an example in your answer. TYPE 2 N4
while count < 5: with count = count + 1, the update line must be indented as part of the loop body. If it were placed after the loop instead, it would only run once the loop had already finished, and the loop would never actually see count change — it would run forever.8. Practical: In IDLE, create a new file called sdd5_loops.py. Write a program that uses a while loop to print the numbers 1 to 5, then stop. Test it and record your output. TYPE 3
count = 1
while count <= 5:
print(count)
count = count + 1Expected output: the numbers 1 to 5, each on its own line.
9. Practical: In sdd5_loops.py, write a second program using a sentinel-controlled while loop that keeps asking "Enter a word, or type 'stop' to finish:" until the user types stop. Record your test output. TYPE 3
word = input("Enter a word, or type 'stop' to finish: ")
while word != "stop":
word = input("Enter a word, or type 'stop' to finish: ")
print("Finished")Expected behaviour: the program keeps asking until "stop" is typed, then prints "Finished".
Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 10 min; now you try 5 min; task set 20 min.
Key misconception to address: "while loops always run at least once" — demonstrate a while loop whose condition is false from the start so pupils see the body never executes.
Live demo suggestion: deliberately write an infinite loop on the board/screen and run it, then use Ctrl+C in the Shell to stop it, so pupils recognise the symptom (program appears frozen) and the fix.
Extension idea (S5 / N5-bridging, non-assessed): briefly mention validation loops (looping until valid input is given) as a preview of N5 standard algorithms — keep clearly separate from the assessed task set.
Assessment standards covered: N3 O2.1; N4 O1.2, O2.1.