Nested Loops

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:18 PM on 10/13/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

11 Terms

1
New cards

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.


<ul><li><p><span>A loop inside another loop</span></p></li><li><p class="text-body text-secondary">The inner loop sits in the indented block of the outer loop, so it is part of what the outer loop repeats.</p></li></ul><p></p>
2
New cards

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.


<ul><li><p><span>Runs all the way through, from start to finish</span></p></li><li><p class="text-body text-secondary">The inner loop completes every one of its rounds before the outer loop moves on to its next value.</p></li></ul><p></p>
3
New cards

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.


4
New cards

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.


5
New cards

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.


<ul><li><p><span>0 0, then 0 1, then 1 0</span></p></li><li><p class="text-body text-secondary">j goes through 0 and 1 while i stays at 0. Only then does i become 1 and j start again at 0.</p></li></ul><p></p>
6
New cards

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.


7
New cards

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.


8
New cards

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.


9
New cards

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.


10
New cards

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.


<ul><li><p><span>Ends the inner loop only; the outer loop carries on</span></p></li><li><p class="text-body text-secondary">break only affects the loop it is directly inside. The outer loop moves to its next round and the inner loop starts again.</p></li></ul><p></p>
11
New cards

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.


<ul><li><p><span>0 0, then 1 0, then 2 0</span></p></li><li><p class="text-body text-secondary">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.</p></li></ul><p></p>