Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
✗ Incorrect
The outer loop starts and runs one iteration, then the inner loop runs completely before the outer loop continues.
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
✗ Incorrect
If the conditions controlling the loops never become false, the loops will run forever, causing an infinite loop.
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
✗ Incorrect
The inner loop runs 3 times for each of the 4 outer loop iterations, so 4 x 3 = 12 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
✗ Incorrect
Option A correctly initializes counters and uses nested while loops with proper increments.
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
✗ Incorrect
The outer loop runs for x=1 and x=2. For each x, the inner loop runs y=1 and y=2, printing pairs in order.
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.
Practice
(1/5)
1. What does a nested while loop do in Python? while condition1: while condition2: # code
easy
A. Runs the inner loop completely for each iteration of the outer loop
B. Runs both loops only once
C. Runs the outer loop only after the inner loop finishes all iterations
D. Runs the inner loop only if the outer loop condition is false
Solution
Step 1: Understand nested loops structure
The outer loop runs while condition1 is true. For each iteration of this outer loop, the inner loop runs fully while condition2 is true.
Step 2: Explain the inner loop behavior
The inner loop completes all its iterations before the outer loop moves to the next iteration.
Final Answer:
Runs the inner loop completely for each iteration of the outer loop -> Option A
Quick Check:
Inner loop runs fully inside outer loop [OK]
Hint: Inner loop finishes all cycles before outer loop continues [OK]
Common Mistakes:
Thinking both loops run only once
Confusing order of loop execution
Assuming inner loop runs only if outer loop is false
2. Which of the following is the correct syntax for a nested while loop in Python?
easy
A. while x < 3:
while y < 2:
print(x, y)
y += 1
B. while x < 3
while y < 2:
print(x, y)
y += 1
C. while x < 3:
while y < 2:
print(x, y)
y += 1
D. while x < 3:
while y < 2:
print(x, y)
y += 1
Solution
Step 1: Check indentation and colons
Python requires a colon after while conditions and proper indentation for nested blocks.
Step 2: Identify correct indentation and syntax
while x < 3:
while y < 2:
print(x, y)
y += 1 uses colons and indents the inner while and print statements correctly.
Final Answer:
while x < 3:
while y < 2:
print(x, y)
y += 1 -> Option D
Quick Check:
Correct colons and indentation = while x < 3:
while y < 2:
print(x, y)
y += 1 [OK]
Hint: Look for colons and consistent indentation [OK]
Common Mistakes:
Missing colons after while statements
Incorrect indentation of inner loop
Mixing indentation levels
3. What is the output of this code?
i = 1
while i <= 2:
j = 1
while j <= 3:
print(i, j)
j += 1
i += 1
medium
A. 1 1\n1 2\n2 1\n2 2
B. 1 1\n2 1\n3 1
C. 1 1\n1 2\n1 3\n2 1\n2 2\n2 3
D. 1 1\n1 2\n1 3
Solution
Step 1: Trace outer loop iterations
i starts at 1 and runs while i <= 2, so i = 1 and i = 2.
Step 2: Trace inner loop iterations for each i
For each i, j runs from 1 to 3, printing i and j each time.
Final Answer:
1 1\n1 2\n1 3\n2 1\n2 2\n2 3 -> Option C
Quick Check:
Inner loop prints 3 times per outer loop iteration [OK]
Hint: Inner loop runs fully for each outer loop iteration [OK]
Common Mistakes:
Only printing inner loop once
Mixing up i and j values
Stopping loops too early
4. Find the error in this nested while loop code:
i = 0
while i < 2:
j = 0
while j < 2:
print(i, j)
j += 1
i += 1
medium
A. No error, code runs fine
B. Inner loop print and increments are not indented properly
C. Variables i and j are not initialized
D. Outer loop condition is incorrect
Solution
Step 1: Check indentation inside inner while loop
The print and j increment lines must be indented inside the inner while loop to run repeatedly.
Step 2: Identify effect of wrong indentation
Without indentation, print and j += 1 run only once, causing an infinite loop or logic error.
Final Answer:
Inner loop print and increments are not indented properly -> Option B
Quick Check:
Indent inner loop body correctly [OK]
Hint: Indent inner loop body to avoid logic errors [OK]
5. You want to print a 3x3 grid of numbers where each cell shows the sum of its row and column indexes (starting from 1). Which nested while loop code correctly does this?
hard
A. row = 1
while row <= 3:
col = 1
while col <= 3:
print(row + col, end=' ')
col += 1
print()
row += 1
B. row = 1
while row <= 3:
col = 1
while col <= 3:
print(row * col, end=' ')
col += 1
print()
row += 1
C. row = 0
while row < 3:
col = 0
while col < 3:
print(row + col, end=' ')
col += 1
print()
row += 1
D. row = 1
while row < 3:
col = 1
while col < 3:
print(row + col, end=' ')
col += 1
print()
row += 1
Solution
Step 1: Check loop ranges for 1 to 3
We want rows and columns from 1 to 3 inclusive, so conditions should be <= 3 starting at 1.
Step 2: Verify sum calculation and printing format
row = 1
while row <= 3:
col = 1
while col <= 3:
print(row + col, end=' ')
col += 1
print()
row += 1 sums row and col indexes and prints with space, then prints a newline after each row.
Final Answer:
row = 1
while row <= 3:
col = 1
while col <= 3:
print(row + col, end=' ')
col += 1
print()
row += 1 -> Option A
Quick Check:
Correct ranges and sum print = row = 1
while row <= 3:
col = 1
while col <= 3:
print(row + col, end=' ')
col += 1
print()
row += 1 [OK]
Hint: Use <= 3 and start from 1 for correct grid indexes [OK]