12-nesting.py
python-programming / programming-constructs  ·  Lesson 13
Lesson 13 — Programming constructs

Nesting Selection & Iteration

Place selection or iteration inside another programming construct.

Nesting means placing one construct inside another. The inner block is indented again so its relationship is clear.

Predict

Read the code first. Do not run it yet.

nesting-predict.py
for row in range(2):
    for column in range(3):
        print(row, column)
Predict before running
  • How many times does the inner loop run for each outer-loop repetition?
  • How many lines are printed altogether?
  • Predict the first and last pair printed.

Investigate

Use this code to explain exactly how the program works.

nesting-investigate.py
score = 75
submitted = True
if submitted:
    if score >= 70:
        print("Distinction")
    else:
        print("Submitted")
else:
    print("Missing")
Investigate the code
  • Which condition is tested first?
  • Which conditions must both lead to the inner print?
  • How does indentation show the nesting?

Nested loops

grid.py
for row in range(3):
    for column in range(2):
        print("Cell", row, column)
How it works
  • The outer loop controls rows.
  • For every row, the inner loop runs through all columns.
  • The inner statement therefore runs 3 × 2 = 6 times.
Read indentation carefullyEach extra level of indentation belongs to the construct directly above it.

Self-study guide

Nesting means placing one programming construct inside another. Common examples are a selection inside a loop, a loop inside a selection, or one loop inside another. Indentation in Python shows exactly which statements belong to which structure.

OCR requires students to understand nesting for selection and iteration. The logic is the same across languages even though OCR ERL uses closing keywords such as endif and next, while Python relies on indentation.

OCR focusBe able to follow and construct nested selection and iteration. Trace from the outside structure inward and keep careful track of which statements repeat.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
if ... then ... endifif ...: with indentationNested blocks are closed explicitly in OCR ERL and by indentation in Python.
for ... to ... nextfor ... in range(...):An inner loop completes for every relevant outer-loop repetition.
nested decisionindented `if` inside another blockThe inner condition is only reached when execution enters its outer block.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Nested loops are easiest to understand by counting how many times the inner body executes.

lesson-13-worked.py
for row in range(1, 4):
    for column in range(1, 5):
        print(row, column)
Output
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
Work through it line by line
  • The outer loop produces three row values.
  • For each row, the inner loop produces four column values.
  • The print statement therefore executes 3 × 4 = 12 times.
  • The inner loop restarts from its first value whenever the outer loop moves to a new row.

Another example

Selection can be nested inside iteration to make a decision for every repeated item.

lesson-13-example-2.py
for number in range(1, 7):
    if number % 2 == 0:
        print(number)
Output
2
4
6
Why this example matters
  • The loop generates every number from 1 to 6.
  • The if condition is checked once for every generated number.
  • Only even values reach the output.

Common mistakes

Avoid these errors
  • Losing track of indentation and accidentally putting code outside the intended block.
  • Assuming the inner loop runs only once in total rather than once per outer iteration.
  • Resetting a running value at the wrong nesting level.
Exam-safe warningWhen tracing nested code, identify the outer block first. Then trace the entire inner block before advancing the outer loop.

Exam-ready checklist

You should now be able to
  • Trace nested loops and selections.
  • Calculate how many times an inner statement executes.
  • Place initialisation at the correct nesting level.
  • Translate nested OCR ERL structures into correctly indented Python.

What you have learned

  • Selection and iteration can be nested.
  • A nested inner loop runs completely for each relevant outer-loop repetition.
  • Indentation makes the structure visible.