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.
for row in range(2):
for column in range(3):
print(row, column)- 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.
score = 75
submitted = True
if submitted:
if score >= 70:
print("Distinction")
else:
print("Submitted")
else:
print("Missing")- Which condition is tested first?
- Which conditions must both lead to the inner print?
- How does indentation show the nesting?
Nested loops
for row in range(3):
for column in range(2):
print("Cell", row, column)- The outer loop controls rows.
- For every row, the inner loop runs through all columns.
- The inner statement therefore runs
3 × 2 = 6times.
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 Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
if ... then ... endif | if ...: with indentation | Nested blocks are closed explicitly in OCR ERL and by indentation in Python. |
for ... to ... next | for ... in range(...): | An inner loop completes for every relevant outer-loop repetition. |
nested decision | indented `if` inside another block | The inner condition is only reached when execution enters its outer block. |
Worked example
Nested loops are easiest to understand by counting how many times the inner body executes.
for row in range(1, 4):
for column in range(1, 5):
print(row, column)1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4
- 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.
for number in range(1, 7):
if number % 2 == 0:
print(number)2 4 6
- The loop generates every number from 1 to 6.
- The
ifcondition is checked once for every generated number. - Only even values reach the output.
Common mistakes
- 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-ready checklist
- 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.