Unit 11.1A · Term 1

Nested Loops

A nested loop is a loop inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop. Nested loops are essential for working with 2D patterns, tables, and grids.

Learning Objectives

  • 11.1.2.7 Use nested loops when solving problems

Lesson Presentation

11.1A-lesson-07-nested-loops.pdf · Slides for classroom use

Conceptual Anchor

The Clock Analogy

Think of a clock: the minute hand (inner loop) completes 60 ticks for every 1 tick of the hour hand (outer loop). After 60 minutes → the hour hand moves by 1 → the minute hand starts again from 0. That's exactly how nested loops work.

Rules & Theory

How Nested Loops Execute

# Structure for i in range(outer): # Outer loop for j in range(inner): # Inner loop print(i, j) # Runs outer × inner times # Example: 3 × 4 = 12 total iterations for i in range(3): for j in range(4): print(f"({i},{j})", end=" ") print() # New line after each row # Output: # (0,0) (0,1) (0,2) (0,3) # (1,0) (1,1) (1,2) (1,3) # (2,0) (2,1) (2,2) (2,3)
Concept Description Example
Total iterations outer_count × inner_count 3 × 4 = 12
Outer variable Changes slowly (once per full inner cycle) i in rows
Inner variable Changes quickly (every single iteration) j in columns
Dependent range Inner range can depend on outer variable range(i+1)

Key Insight

For every single value of i, the inner loop runs completely from start to finish. Only then does i move to the next value.

Worked Examples

1 Star Rectangle

rows = 4 cols = 6 for i in range(rows): for j in range(cols): print("*", end=" ") print() # Output: # * * * * * * # * * * * * * # * * * * * * # * * * * * *

2 Right Triangle Pattern

# Inner range depends on outer variable for i in range(1, 6): for j in range(i): print("*", end=" ") print() # Output: # * # * * # * * * # * * * * # * * * * * # Trace: # i=1 → j runs 0 → 1 star # i=2 → j runs 0,1 → 2 stars # i=3 → j runs 0,1,2 → 3 stars # i=4 → j runs 0,1,2,3 → 4 stars # i=5 → j runs 0,1,2,3,4 → 5 stars

3 Multiplication Table (10×10)

# Header row print("\t", end="") for j in range(1, 11): print(j, end="\t") print() print("-" * 90) # Table body for i in range(1, 11): print(i, end="\t") for j in range(1, 11): print(i * j, end="\t") print() # Output: # 1 2 3 4 5 6 7 8 9 10 # -------... # 1 1 2 3 4 5 6 7 8 9 10 # 2 2 4 6 8 10 12 14 16 18 20 # ...

4 Number Pyramid

n = 5 for i in range(1, n + 1): # Print spaces for alignment for s in range(n - i): print(" ", end="") # Print numbers for j in range(1, i + 1): print(j, end="") print() # Output: # 1 # 12 # 123 # 1234 # 12345

Pitfalls & Common Errors

Using the Same Variable Name

Using i for both outer and inner loops causes the outer variable to be overwritten. Always use different names (i/j, row/col).

Misplaced print()

print() for a new line must be inside the outer loop but outside the inner loop. If it's inside the inner loop, you get one value per line instead of rows.

Performance Issues

Nested loops multiply iterations. A loop of 1000 inside a loop of 1000 = 1,000,000 iterations. Three nested loops of 100 each = 1,000,000. Be careful with large ranges.

Pro-Tips for Exams

Pattern Questions

  • For any pattern: first identify how many rows and what changes each row
  • Inner range depending on outer variable = triangle/staircase patterns
  • Spaces + characters = centered/pyramid patterns
  • Always trace the first 2-3 rows manually before writing code
  • Total iterations = sum of inner loop counts for each outer value

Graded Tasks

Remember

How many total iterations occur in: for i in range(5): for j in range(3): print(i, j)?

Understand

Explain why print() is placed after the inner loop but inside the outer loop when printing patterns.

Apply

Write a nested loop to print an inverted triangle: 5 stars on line 1, 4 on line 2, ..., 1 on line 5.

Apply

Write a program that prints all pairs (i, j) where i + j = 10 and both i, j are between 1 and 9.

Analyze

Trace: for i in range(3): for j in range(i, 3): print(j, end=""); print(). Show the output.

Create

Create a diamond pattern of stars with height 7 (3 rows growing, 1 middle, 3 rows shrinking).

Self-Check Quiz

1. In a nested loop, which loop completes first?
Click to reveal answer
2. How many times does print() execute in: for i in range(4): for j in range(5): print("X")?
Click to reveal answer
3. What pattern does for i in range(1, n+1): print('*' * i) produce?
Click to reveal answer
4. Can the inner loop's range depend on the outer loop variable?
Click to reveal answer
5. What happens if both loops use the same variable name i?
Click to reveal answer