New
New
Year 10
OCR

Nested loops

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

New
New
Year 10
OCR

Nested loops

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

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.

These resources were created for remote use during the pandemic and are not designed for classroom teaching.

Lesson details

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.


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

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.
Teacher tip

Equipment

Code in this lesson is made using Python. Code examples use the RPF Code Editor but any Python IDE is suitable.

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.
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

When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:


  • • Blue - numbers and boolean values
  • • Green - strings
  • • Purple - keywords
1 - 0 0
2 - 0 1
3 - 0 2
4 - 1 0
5 - 1 1
6 - 1 2