1/10
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is a nested loop?
A loop inside another loop
The inner loop sits in the indented block of the outer loop, so it is part of what the outer loop repeats.

For each single round of the outer loop, what does the inner loop do?
Runs all the way through, from start to finish
The inner loop completes every one of its rounds before the outer loop moves on to its next value.

r i in range(3): then inside it for j in range(2): then print(i, j). How many times does print run?
6
The outer loop runs 3 times and the inner loop runs 2 times for each of those: 3 x 2 = 6.
How do you work out how many times the innermost code runs in a nested loop?
Multiply the outer count by the inner count
An outer loop of 4 with an inner loop of 5 runs the innermost code 4 x 5 = 20 times.
for i in range(3): for j in range(2): print(i, j). What are the first three lines printed?
0 0, then 0 1, then 1 0
j goes through 0 and 1 while i stays at 0. Only then does i become 1 and j start again at 0.

In a nested loop, which variable changes more often?
The inner loop's variable
The inner variable runs through all its values for every one value of the outer variable, like minutes versus hours.
When the outer loop starts a new round, where does the inner loop begin?
From its first value again
The inner loop restarts from scratch each time. With range(2), j goes 0, 1 again on every outer round.
Outer loop range(3), inner loop range(2). A line is indented once, so it is inside the outer loop but not the inner. How many times does it run?
3 times
It belongs only to the outer loop, so it runs once per outer round: 3 times.
or a in range(1, 4): for b in range(1, 4): print(a * b). What are the first three numbers printed?
1, 2, 3
a stays at 1 while b goes 1, 2, 3, giving 1x1, 1x2, 1x3. Then a becomes 2 and you get 2, 4, 6.
What does break do when it is inside the inner loop?
Ends the inner loop only; the outer loop carries on
break only affects the loop it is directly inside. The outer loop moves to its next round and the inner loop starts again.

for i in range(3): for j in range(3): if j == 1, break. Then print(i, j). What prints?
0 0, then 1 0, then 2 0
On each outer round, j = 0 prints, then j = 1 hits break and the inner loop ends. The outer loop still runs all 3 rounds.
