Programs · Program 6-16
Q.Program 6-16: Program to demonstrate working of nested for loops.
Sikkim CbseNCERTSubjective· 3mImportance★★★★★est
51% · 20/39 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A loop placed inside another loop is nested: the inner loop restarts and runs to completion for every pass of the outer loop — here 3 outer × 3 inner = 9 printed pairs.
The idea. Whenever work has a two-dimensional shape — rows and columns, tables, patterns — one loop handles the rows and a second loop inside it handles the columns. Understanding the execution order (outer advances once only after the inner finishes a full cycle) is the whole point of this demonstration.
# Program 6-16: demonstrate the working of nested for loops
for i in range(1, 4): # outer loop: i = 1, 2, 3
for j in range(1, 4): # inner loop: j = 1, 2, 3 for EACH i
print(i, j)
Expected output
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Dry run
Outer i | Inner j values run | Lines printed |
|---|---|---|
| 1 | 1, 2, 3 | 1 1, 1 2, 1 3 |
| 2 | 1, 2, 3 | 2 1, 2 2, 2 3 |
| 3 | 1, 2, 3 | 3 1, 3 2, 3 3 |
Total body executions = 3 × 3 = 9 — in general, (outer iterations) × (inner iterations). …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.