N3/N4  ·  SDD  ·  Software Design & Development

Conditional Loops (while)

National 3 & National 4 Computing Science Lesson SDD5 of 10 Approx 55 min
Learning intentions
  • Understand what a conditional loop is and how it differs from a fixed loop
  • Use while to repeat instructions until a condition becomes false
  • Avoid writing an infinite loop
Success criteria
  • I can write a while loop 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
Warm up — what do you already know?

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 loop
A loop that repeats for as long as a condition is true, rather than a fixed number of times.
while
Python's keyword for writing a conditional loop.
Condition
A true/false test that controls whether a while loop keeps repeating.
Infinite loop
A loop whose condition never becomes false, so it never stops — usually a bug.
Loop control variable
A variable that is checked in the condition and must be updated inside the loop for it to eventually stop.
Sentinel value
A special input value (like "quit") used to signal that a loop should stop.

Conditional Loops (while)

What is a conditional loop?

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

Example 1 — a basic while loop

A program prints "Still going" while a counter is below 3.

1
count = 0
2
while count < 3:
    print("Still going")
    count = count + 1
3
This prints "Still going" three times. Each pass, count increases by 1 (0 → 1 → 2 → 3), and once count reaches 3, count < 3 is false, so the loop stops.
Example 2 — an infinite loop bug

A program that never stops.

1
count = 0
2
while count < 3:
    print("Still going")
3
This never stops. 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.
Example 3 — a sentinel-controlled loop

A program keeps asking for a password until the correct one is entered.

1
password = input("Enter the password: ")
2
while password != "python123":
    password = input("Incorrect. Try again: ")
3
The loop keeps asking for input as long as it doesn't match "python123". The input() call inside the loop is what updates password — without it, this would also become an infinite loop.
Now you try

A program contains: total = 0, then while total < 10: with total = total + 3 indented underneath, then print(total) after the loop.

Answer the following:

  1. What values does total take across each pass of the loop?
  2. How many times does the loop run?
  3. What value is printed at the end?
  1. total goes 0 → 3 → 6 → 9 → 12.
  2. 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).
  3. 12 is printed — the loop stops as soon as total reaches or passes 10, it doesn't stop exactly at 10.
Common mistakes
Forgetting to update the loop control variable. If nothing inside the loop changes the value being checked, the condition never becomes false, and the program hangs forever.
Updating the variable outside the loop. Just like a running total, an update line placed after the loop body (not indented with it) never actually runs repeatedly — it only runs once.
Assuming a while loop always runs at least once. Python checks the condition before the first pass — if it's false immediately, the loop body never runs at all.
Using = instead of == in the condition. Just like with if, a while condition needs the comparison operator ==, not the assignment operator =.
Tip

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.

Task Set

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

An infinite loop is a loop whose condition never becomes false, so it repeats forever and the program never moves on. This commonly happens when the loop control variable — the value being checked in the condition — is never updated inside the loop, so the condition stays true no matter how many times the loop repeats.

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

The condition is re-checked before every pass of the loop, so the variable must change during each pass for the loop to eventually stop. For example, in 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 + 1


Expected 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".
Teacher notes — Shift+T to hide

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.