Choose exam board for KS4 Computer Science (GCSE)
Choose exam board for KS4 English
Choose exam board for KS4 French
Choose exam board for KS4 Geography
Choose exam board for KS4 German
Choose exam board for KS4 History
Choose tier for KS4 Maths
Choose exam board for KS4 Music
Choose exam board for KS4 Physical education (GCSE)
Choose exam board for KS4 Religious education (GCSE)
Choose exam board for KS4 Spanish

      Iteration using while loops

      Lesson details

      Learning outcome

      I can use iteration to repeat sequences of code in a program and use a flag to stop a loop from running.

      Key learning points

      1. Condition controlled iteration will execute until a condition is met.
      2. While loops are used for condition controlled iteration in Python.
      3. Flags can be used to start and stop a loop in a program.

      Keywords

      • Iteration - the process of repeating a sequence of instructions within a program loop

      • Condition - an expression that evaluates to True or False

      • Flag - a signal that is used to let a program know whether a condition has been met or not

      Common misconception

      A while loop is the most appropriate loop to use when you know exactly how many iterations will need to be performed.

      While loops should be used when the number of iterations can change. Even though while loops can be used in this way, for loops are a more appropriate choice as they will track the number of iterations as part of their operation.

      Teacher tip

      The number of times a program iterates through a sequence of code can sometimes be confusing for pupils. Encourage them to test programs and reflect on how many times the loop is executed. They could use debugging tools or by outputting a counting variable.

      Equipment

      Pupils will need access to a device that can edit and run Python programs. Examples in this lesson use the RPF Code Editor https://oak.link/python-editor

      Licence

      This content is © Oak National Academy Limited (2026), licensed on Open Government Licence version 3.0
      except where otherwise stated. See Oak's terms & conditions
      (Collection 2).

      Lesson video

      Loading...

      Prior knowledge starter quiz

      6 Questions

      Q1.
      You can use multiple elif blocks.

      Correct answer: True
      False

      Q2.
      A program calculates a theme park ticket price by first checking the users age and then checking if it is peak season or not. Which of the following is used in the program?

      single if
      if-else
      Correct answer: nested `if`s
      while loop

      Q3.
      Match the relational operator to it's description.

      Correct Answer:>,greater than

      greater than

      Correct Answer:<,less than

      less than

      Correct Answer:!=,not equal to

      not equal to

      Correct Answer:==,equal to

      equal to

      Correct Answer:<=,less than or equal to

      less than or equal to

      Correct Answer:>=,more than or equal to

      more than or equal to

      Q4.
      What will be the output of the following Python code?
      12345678
      score = 50 if score >= 80: print("Win") elif grade >= 50: print("Try again") else: print("You lose")
      Code colour

      Win
      Correct answer: Try again
      You lose

      Q5.
      Which of the following statements about nested if statements is true?

      Correct answer: Nested `if` statements allow you to make more complex decisions within your code
      You can only have one level of nesting in Python.
      Nested if statements execute all code blocks, regardless of the condition.
      Indentation is not important when using nested if statements.

      Q6.
      A school organises students based on age. Students 11-16 attend High School, and students 16-18 Sixth Form. Which expression correctly checks if a student's age falls within the High School range?

      age > 11 and age < 16
      age >= 11 and age <= 16
      Correct answer: age > 11 or age < 16
      age >= 11 or age <= 16

      5 Questions

      Q1.
      What does 'iteration' mean in programming?

      making choices based on conditions
      Correct answer: repeating a set of instructions multiple times
      storing data that can change
      generating random values

      Q2.
      What is the purpose of a flag in programming?

      to store a number
      Correct answer: to indicate whether a specific condition has been met
      to perform mathematical calculations
      to display output on the screen

      Q3.
      What while loop should you use if you want a block of code to run forever?

      Correct answer: `while True:`
      while Forever:
      while False:
      while x <10:

      Q4.
      In Python, which of the following is a way to create a flag variable named is_game_over and set its initial value to False?

      is_game_over = "False"
      Correct answer: `is_game_over = False`
      is_game_over = 0
      is_game_over = "no"

      Q5.
      What is treasure_found in this code?
      12345
      distance_traveled = 0 treasure_found = False while treasure_found == False: print(f"Keep going {distance_traveled} steps travelled so far")
      Code colour

      an integer
      Correct answer: a flag
      a loop
      user input

      To help you plan your 8 computing lesson on: Iteration using while loops, download all teaching resources for free and adapt to suit your pupils' needs...