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

      Lesson details

      Learning outcome

      I can create programs that use nested loops to solve problems.

      Key learning points

      1. Nested loops involve placing one loop inside another.
      2. The outer loop controls how many times the inner loop repeats.
      3. Each time the outer loop is repeated, the inner loops are re-entered and started again as if new.

      Keywords

      • Nested loop - the process of placing one loop inside another loop

      • Outer loop - the loop that contains another loop inside it

      • Inner loop - the loop that is placed inside another loop and runs completely each time the outer loop runs once

      Common misconception

      Learners may think that the inner loop only runs once per program, rather than repeating fully each time the outer loop runs.

      The inner loop runs completely every time the outer loop runs once. This means the code inside the inner loop is repeated multiple times depending on how many times the outer loop is executed.

      Teacher tip

      Trace the code with learners step by step and clearly label each iteration to show how the inner loop restarts each time the outer loop runs.

      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.
      What is iteration in programming?

      the process of writing a program
      the process of debugging a program
      the process of converting code into machine language
      Correct answer: the process of repeating a sequence of instructions

      Q2.
      What is a count-controlled iteration?

      a loop that runs while a condition is True
      Correct answer: a loop that repeats for a specific number of times
      a loop that runs indefinitely
      a loop that runs until a file is closed

      Q3.
      Which Python statement is used for count-controlled iteration?

      Correct Answer: for

      Q4.
      What does the range() function do in Python?

      It creates a list of strings.
      It checks a condition in a loop.
      Correct answer: It generates a sequence of numbers.
      It stops a loop from running.

      Q5.
      Which Python statement is used to repeatedly execute a block of code as long as a condition remains True?

      Correct Answer: while

      Q6.
      Why is indentation important in Python?

      Correct answer: It defines blocks of code.
      It makes the code run faster.
      It helps convert code to machine language.
      It is used to name variables.

      6 Questions

      Q1.
      What is a nested loop?

      Correct answer: a loop placed inside another loop
      a loop that runs multiple times
      a loop that runs infinitely
      a loop that runs conditionally

      Q2.
      What happens to the inner loop when the outer loop runs again?

      It continues from where it left off.
      Correct answer: It starts again as if new.
      It skips to the next iteration.
      It only runs once.

      Q3.
      Which of these is an example of a nested loop?

      for i in range(5): print(i)
      while True: print("Hello")
      Correct answer: `for i in range(5): for j in range(3): print(i, j)`
      for i in range(5): print(i, j)

      Q4.
      What is the purpose of a nested loop?

      to create infinite loops
      Correct answer: to repeat a process within another process
      to simplify code
      to reduce iterations

      Q5.
      What happens if the outer loop in a nested loop has a range of 0?

      the inner loop runs once
      the inner loop runs infinitely
      Correct answer: the inner loop doesn’t run at all
      the program throws an error

      Q6.
      Arrange the output for the following code:
      123
      for i in range(2): for j in range(3): print(i, j)
      Code colour

      1 - 0 0
      2 - 0 1
      3 - 0 2
      4 - 1 0
      5 - 1 1
      6 - 1 2

      To help you plan your 10 computer science lesson on: Nested loops, download all teaching resources for free and adapt to suit your pupils' needs...