Learning outcomes
By the end of this lecture, you should be able to:- build triangular and number patterns
- control row-dependent inner loop boundaries
- trace nested loops with confidence
Pattern 1: right triangle of stars
Pattern 2: number triangle
Pattern 3: multiplication grid
Row-dependent boundaries
Key idea:- inner loop range often depends on
i - this is what creates triangles and pyramids
Debug trap
Wrong:Fix: inner range should be
range(1, i + 1).
Complexity intuition
- triangular nested loops often perform about
n(n+1)/2steps - full grid loops perform
n*nsteps
Exam hints and traps
- check whether inner loop limit is fixed or row-dependent
- verify
+1inclusion in ranges - for pattern questions, write small dry-run for
n = 3
Practice
- Print inverted star triangle.
- Print pattern:
- Write nested loop to print table from
2to4side by side.
