icon-background-square
New
New
Year 8

Iteration using while loops

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

icon-background-square
New
New
Year 8

Iteration using while loops

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

warning

These resources will be removed by end of Summer Term 2025.

Switch to our new teaching resources now - designed by teachers and leading subject experts, and tested in classrooms.

Lesson details

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.


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

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.
speech-bubble
Teacher tip
equipment-required

Equipment

All pupils require access to devices that can edit and run Python programs. Starter code files are available to copy or use directly via the Raspberry Pi Code Editor.

copyright

Licence

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

Lesson video

Loading...

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
tick

greater than

Correct Answer:<,less than
tick

less than

Correct Answer:!=,not equal to
tick

not equal to

Correct Answer:==,equal to
tick

equal to

Correct Answer:<=,less than or equal to
tick

less than or equal to

Correct Answer:>=,more than or equal to
tick

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")
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")
an integer
Correct answer: a flag
a loop
user input