Learning Objectives
- Explain nesting and identify levels of nesting.
- Write selection inside loops and loops inside selection.
- Use nested loops for repeated groups and arrays.
- Trace nested structures without losing control flow.
Key Terms
- Nested statement
- A control structure placed inside another control structure.
- Outer structure
- The containing selection or loop.
- Inner structure
- The structure executed within the outer body or branch.
- Nesting level
- The depth of one structure inside another.
- Nested iteration
- A loop inside another loop.

Why Nest Statements
Real problems often require a decision during repetition or repetition only in one branch. Nesting combines structures so the program can express this logic.
Cambridge includes nested selection and iteration and will not require more than three levels of nested statements. Indentation is essential because each ENDIF, ENDWHILE or NEXT must be matched with the correct opening statement.
Nesting should be used only when it reflects the problem. Excessively deep structures reduce readability and may indicate that procedures would be clearer.
Selection Inside Iteration
The outer FOR repeats twenty times. The inner IF is evaluated once in each iteration. PassCount increases only for qualifying marks.
PassCount <- 0
FOR Index <- 1 TO 20
INPUT Mark
IF Mark >= 50
THEN
PassCount <- PassCount + 1
ENDIF
NEXT Index
Iteration Inside Selection
The loop runs only when the IF condition is true. Otherwise the program executes the ELSE branch and skips the loop.
IF NumberOfItems > 0
THEN
FOR Index <- 1 TO NumberOfItems
INPUT Price
NEXT Index
ELSE
OUTPUT "No items"
ENDIF
Nested Loops
With one loop inside another, the inner loop completes all its iterations for each single outer iteration. If the outer loop runs 3 times and the inner loop runs 4 times, the inner body executes 12 times.
Nested loops are required to process 2D arrays. The outer loop can represent rows and the inner loop columns, visiting every element once.
Use different loop variables such as Row and Column. Reusing the same variable in both loops makes the control ambiguous and incorrect.
Tracing Nested Structures
Trace from the outside inward. Record the outer variables, then every inner iteration before advancing the outer loop. A table may need one row for every execution of the innermost statement.
When selection is nested, evaluate the outer condition first. An inner condition is not reached if its containing branch is skipped.
Execution Counts
| Outer Iterations | Inner Iterations Per Outer | Innermost Executions |
|---|---|---|
| 2 | 5 | 10 |
| 4 | 3 | 12 |
| N | M | N * M |
Worked Examples
Multiplication Grid
Question: Use nested loops to output products for rows 1 to 3 and columns 1 to 4.
- Outer loop Row from 1 to 3.
- Inner loop Column from 1 to 4.
- Output Row * Column in the inner body.
- Complete all columns before moving to the next row.
Answer: The product output executes 12 times.
Valid Positive Values
Question: For ten inputs, add a value only when it is positive.
- Use a FOR loop for ten values.
- Input inside the loop.
- Nest IF Value > 0 around the total update.
Answer: Selection inside iteration ensures only positive values are totalled.
Examination Guidance
- Indent every nested level consistently.
- Match closing keywords to the correct opening structures.
- Use different variables for nested loops.
- Calculate total inner executions by multiplying loop counts.
- Remember an inner condition is reached only when the outer path enters it.
Common Mistakes
- Assuming the inner loop runs once in total rather than once per outer iteration.
- Reusing Index for both loops.
- Closing the outer structure before the inner structure.
- Forgetting that an inner IF is evaluated each loop iteration.
- Creating unnecessary deep nesting instead of using procedures.
Knowledge Check
1. What is nesting?
2. How many nesting levels may candidates be required to write?
3. How often does an inner loop run?
4. Why use different loop variables?
5. What structure processes rows and columns of a 2D array?