0
0
Pythonprogramming~5 mins

Nested while loops in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a nested while loop?
A nested while loop is a while loop inside another while loop. The inner loop runs completely for each iteration of the outer loop.
Click to reveal answer
beginner
How does the flow of execution work in nested while loops?
The outer loop runs once, then the inner loop runs fully. After the inner loop finishes, the outer loop runs its next iteration, and the inner loop runs again, repeating this until the outer loop ends.
Click to reveal answer
beginner
Write a simple example of nested while loops that prints a 3x3 grid of stars.
i = 1 while i <= 3: j = 1 while j <= 3: print('*', end=' ') j += 1 print() i += 1
Click to reveal answer
intermediate
Why should you be careful with nested while loops?
Because if the inner or outer loop conditions never become false, the program can get stuck in an infinite loop, which means it keeps running forever.
Click to reveal answer
beginner
How can nested while loops be useful in real life?
They help when you need to repeat tasks inside other repeated tasks, like printing tables, processing grids, or checking combinations of items.
Click to reveal answer
What happens first in nested while loops?
ABoth loops run at the same time
BThe outer loop runs one iteration, then the inner loop runs fully
CThe inner loop runs once, then the outer loop runs fully
DOnly the outer loop runs
What is a risk when using nested while loops?
AInfinite loops if conditions never become false
BThe program runs too fast
CThe loops skip iterations automatically
DThe loops only run once
How many times does the inner loop run if the outer loop runs 4 times and the inner loop runs 3 times each iteration?
A3 times
B4 times
C12 times
D7 times
Which of these is a correct way to start a nested while loop?
Ai = 0\nwhile i < 2:\n j = 0\n while j < 3:\n print(i, j)\n j += 1\n i += 1
Bwhile i < 2:\n while j < 3:\n print(i, j)\n j += 1\n i += 1
Ci = 0\nj = 0\nwhile i < 2 and j < 3:\n print(i, j)\n i += 1\n j += 1
Dfor i in range(2):\n while j < 3:\n print(i, j)\n j += 1
What will this code print? x = 1 while x <= 2: y = 1 while y <= 2: print(x, y) y += 1 x += 1
A2 2 1 1 2 1 1 2
B1 1 2 1 1 2 2 2
C1 1 1 2 1 3 2 1
D1 1 1 2 2 1 2 2
Explain how nested while loops work and give a simple example.
Think about loops inside loops and how they repeat.
You got /3 concepts.
    Describe a real-life situation where nested while loops could be useful.
    Imagine doing one task many times, and inside each, doing another task many times.
    You got /2 concepts.