Dry Runs, Tracing & Logic Control: Handling Nested Loops During Dry Runs (Copy)
Handling Nested Loops During Dry Runs (Cambridge Standard – O Level 2210 + IGCSE 0478)
Why Nested Loops Are One Of The Hardest Dry-Run Areas In Paper 2
- Nested loops involve:
- A loop running inside another loop
- Cambridge uses nested loops to test:
- Control flow discipline
- Index interaction awareness
- Ability to track repeated executions correctly
- Most students lose marks because they:
- Mix up inner and outer loop indices
- Forget how many times the inner loop runs
- Update variables at the wrong nesting level
Core Principle Of Nested Loops (Non-Negotiable)
- The outer loop controls how many times the inner loop runs
- The inner loop runs fully for every single outer loop iteration
- You must:
- Complete the inner loop entirely
- Before moving to the next outer loop value
Step 1: Identify The Loop Hierarchy Before Tracing
Before starting any dry run:
- Identify:
- Outer loop
- Inner loop
- Label them clearly:
- Outer loop index (e.g. row, i)
- Inner loop index (e.g. col, j)
- Never reuse the same index variable for both loops
Example structure:
- FOR r ← 1 TO 3
- FOR c ← 1 TO 4
- statements
- ENDFOR
- FOR c ← 1 TO 4
- ENDFOR
Step 2: Understand Execution Order In Nested Loops
Execution follows this strict order:
- Outer loop starts (r = first value)
- Inner loop runs from start to end (all c values)
- Inner loop finishes
- Outer loop increments (r moves to next value)
- Inner loop runs again from the beginning
- Process repeats until outer loop ends
Step 3: The “Grid Method” For Dry Runs (Highly Recommended)
For nested loops, always think in rows and columns.
Example bounds:
- r from 1 to 3
- c from 1 to 4
Execution order:
- (1,1) → (1,2) → (1,3) → (1,4)
- (2,1) → (2,2) → (2,3) → (2,4)
- (3,1) → (3,2) → (3,3) → (3,4)
This exact order must be followed in your dry run.
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Step 4: Dry Run Example – Simple Nested Loop (Counting Executions)
Algorithm
- count ← 0
- FOR i ← 1 TO 3
- FOR j ← 1 TO 2
- count ← count + 1
- ENDFOR
- FOR j ← 1 TO 2
- ENDFOR
Dry Run Table
| i | j | count |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 1 | 3 |
| 2 | 2 | 4 |
| 3 | 1 | 5 |
| 3 | 2 | 6 |
Key Observation
- Inner loop runs 2 times
- Outer loop runs 3 times
- Total executions = 3 × 2 = 6
Examiner Trap
- Writing count = 5
- Forgetting one inner loop execution
Step 5: Dry Running Nested Loops With Arrays
Algorithm (2D Array Traversal)
- total ← 0
- FOR r ← 1 TO 2
- FOR c ← 1 TO 3
- total ← total + grid[r,c]
- ENDFOR
- FOR c ← 1 TO 3
- ENDFOR
Given:
- grid =
- Row 1: 1, 2, 3
- Row 2: 4, 5, 6
Dry Run Order And Totals
| r | c | grid[r,c] | total |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 1 | 2 | 2 | 3 |
| 1 | 3 | 3 | 6 |
| 2 | 1 | 4 | 10 |
| 2 | 2 | 5 | 15 |
| 2 | 3 | 6 | 21 |
Examiner Focus
- Correct row-first traversal
- No column skipping
- Correct accumulation order
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Step 6: Nested Loops With Conditional Logic (High-Risk Area)
Algorithm
- count ← 0
- FOR i ← 1 TO 3
- FOR j ← 1 TO 3
- IF i = j THEN
- count ← count + 1
- ENDIF
- IF i = j THEN
- ENDFOR
- FOR j ← 1 TO 3
- ENDFOR
Dry Run Interpretation
Pairs checked:
- (1,1) TRUE → count = 1
- (1,2) FALSE
- (1,3) FALSE
- (2,1) FALSE
- (2,2) TRUE → count = 2
- (2,3) FALSE
- (3,1) FALSE
- (3,2) FALSE
- (3,3) TRUE → count = 3
Final:
- count = 3
Examiner Trap
- Incrementing count for every j
- Ignoring condition placement
Step 7: Nested Loops With Updates (Order Matters)
Algorithm
- FOR i ← 1 TO 3
- FOR j ← 1 TO 3
- values[j] ← values[j] + i
- ENDFOR
- FOR j ← 1 TO 3
- ENDFOR
Initial:
- values = [0, 0, 0]
Dry Run Execution
Outer loop i = 1:
- values → [1, 1, 1]
Outer loop i = 2:
- values → [3, 3, 3]
Outer loop i = 3:
- values → [6, 6, 6]
Key Insight
- Updates accumulate across outer loop iterations
- Inner loop resets j each time
- values persist between outer loop cycles
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Step 8: Nested Loops With WHILE Inside FOR (Mixed Control)
Algorithm
- FOR i ← 1 TO 3
- j ← 1
- WHILE j <= 2 DO
- OUTPUT i, j
- j ← j + 1
- ENDWHILE
- ENDFOR
Output Sequence
- (1,1), (1,2)
- (2,1), (2,2)
- (3,1), (3,2)
Examiner Traps
- Forgetting to reset j inside FOR
- Letting j continue from previous iteration
Step 9: Common Nested Loop Dry Run Mistakes (Guaranteed Mark Loss)
- Skipping inner loop iterations
- Incrementing outer loop too early
- Forgetting inner loop resets
- Mixing index variables
- Applying updates at wrong nesting level
- Assuming inner loop runs once per program instead of per outer loop
Step 10: Nested Loops In Pre-Release Material
- Pre-release tasks often use:
- Nested loops for:
- Table processing
- Grid-based problems
- Multi-category analysis
- Nested loops for:
- Cambridge expects:
- Exact traversal order
- No optimisation shortcuts in dry runs
Safe Pre-Release Dry Run Strategy
- Write all (outer, inner) index pairs
- Track one full outer iteration at a time
- Only move outer loop when inner loop finishes
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Examiner Checklist For Nested Loop Dry Runs
- Identified outer and inner loops correctly
- Inner loop fully executed per outer iteration
- Indices tracked separately
- Updates applied at correct level
- Arrays updated persistently
- Output order correct
Best-Practice Strategy Under Exam Pressure
- Draw a grid or sequence of (i, j) pairs
- Never jump rows or columns
- Trace inner loop fully before moving on
- Reset inner loop variables explicitly
- Treat nested loops as repeated blocks, not shortcuts
Final Quality Checklist
- Correct execution order
- No skipped iterations
- No mixed indices
- Updates applied correctly
- Outputs recorded in order
Final Lock-In Rules
- Nested loops = inner loop completes fully each time
- Execution order is strict and predictable
- Updates persist across outer loops
- Indices must never be confused
- Correct nested-loop tracing = easy Paper 2 marks
