Skip to content
Menu

Year 10 (ages 14–15, Grade 9 in the US) · Computing · GCSE programming · Lesson 5

Condition-controlled loops: while

Using while loops that repeat until a condition changes, validation loops that keep asking until input is valid, counters, sentinel values, and avoiding infinite loops.

Exam boards: this lesson matches the AQA and Pearson Edexcel GCSE computing courses. Check which course your school follows.

Whiteboard video lesson: Condition-controlled loops: while

8-minute video lesson

Teaching, worked examples and 20 practice questions solved step by step.

Watch free for a month

What your child will learn

  • Explain condition-controlled iteration
  • Write while loops
  • Write an input validation loop
  • Avoid infinite loops

The method

  1. while repeats WHILE the condition is True.
  2. Set up the variable before the loop.
  3. Change it inside the loop so the loop can end.
  4. Use for when the count is known; while when it isn't.

Key words: while loop, condition-controlled, validation, infinite loop, sentinel.

Worked examples

Worked examples from the lesson, with every step.

Question 1

What does this output? c:n = 10 c:while n > 0: c: print(n) c: n = n - 3

  1. Step 1: 10, 7, 4, 1
  2. Step 2: Next n = −2 → condition False

Answer: 10, 7, 4, 1

Question 2

Why is this an infinite loop? c:x = 1 c:while x < 5: c: print(x)

  1. Step 1: x never changes.
  2. Step 2: x < 5 is always True.

Answer: x is never updated

Question 3

How many times does the loop run? c:total = 0 c:while total < 20: c: total = total + 6

  1. Step 1: 6, 12, 18, 24
  2. Step 2: 4 passes; 24 is not < 20

Answer: 4