Consider this nested loop: for i in range(3), and inside it for j in range(2), printing (i, j) each iteration. How many lines of output does this produce, and what is the last line printed?
A3 lines; last line is (2, 2)
B5 lines; last line is (2, 1)
C6 lines; last line is (2, 1)
D6 lines; last line is (3, 2)
The outer loop runs 3 times (i = 0, 1, 2). For each value of i, the inner loop runs completely through j = 0 and j = 1 — that is 2 iterations. Total = 3 × 2 = 6 lines. The last pair is when i = 2 (last outer iteration) and j = 1 (last inner iteration). Option D is wrong because range(3) stops at 2, not 3.
Question 2 Multiple Choice
A nested loop has an outer loop that runs 5 times and an inner loop that runs 4 times. How many times does the body of the inner loop execute in total?
A9 times (5 + 4)
B20 times (5 × 4)
C16 times (4 squared)
DIt depends on the specific loop conditions at runtime
For each of the 5 outer iterations, the inner loop completes all 4 of its iterations. The total is 5 × 4 = 20. The additive answer (9) is a common misconception — addition would apply if the loops ran sequentially (first one, then the other), not nested. Nesting creates multiplication because every inner iteration is repeated for every outer iteration.
Question 3 True / False
In a nested loop, the inner loop's counter variable retains its final value from the previous outer iteration when the outer loop advances to the next iteration.
TTrue
FFalse
Answer: False
The inner loop resets to its starting value each time the outer loop advances. The inner loop is a complete, fresh execution each time the outer loop body runs. For example, with `for i in range(3): for j in range(2)`, j always starts at 0 when a new value of i begins — it does not carry over. This reset behavior is essential to understand when tracing execution.
Question 4 True / False
Nested loops are the natural structure for generating every combination of items from two independent sets.
TTrue
FFalse
Answer: True
Because the inner loop runs completely for each outer iteration, every (outer value, inner value) pair is visited exactly once — the full Cartesian product of the two sets. Generating all size-color combinations, visiting every cell in a grid, or checking all pairs of numbers in a list all require this structure.
Question 5 Short Answer
Why does the body of the inner loop execute outer × inner times rather than outer + inner times?
Think about your answer, then reveal below.
Model answer: Because for each single iteration of the outer loop, the entire inner loop runs from start to finish. The outer loop does not advance until the inner loop completes all its iterations. This means every inner iteration is paired with every outer iteration — a multiplicative relationship, not an additive one. Analogy: a clock's minute hand completes 60 rotations for every 1 rotation of the hour hand, giving 60 × 1 total minute-hand positions per hour-hand position.
Addition would apply if the loops were sequential — first loop A runs n times, then loop B runs m times, for n + m total. Nesting creates multiplication because every step of A triggers a complete run of B. This multiplicative structure is also why nested loops can become computationally expensive: two loops of length n produce O(n²) operations, three loops produce O(n³), and so on.