Challenge - 5 Problems
Loop Control Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this loop with break?
Consider the following Python code that uses a loop control statement. What will be printed when this code runs?
Python
for i in range(5): if i == 3: break print(i)
Attempts:
2 left
💡 Hint
The break statement stops the loop immediately when the condition is met.
✗ Incorrect
The loop prints numbers starting from 0. When i equals 3, the break stops the loop, so 3 is not printed.
❓ Predict Output
intermediate2:00remaining
What does continue do in this loop?
Look at this code using the continue statement. What numbers will be printed?
Python
for i in range(5): if i == 2: continue print(i)
Attempts:
2 left
💡 Hint
Continue skips the current loop cycle when the condition is true.
✗ Incorrect
When i is 2, continue skips printing it. All other numbers from 0 to 4 are printed.
❓ Predict Output
advanced2:00remaining
What is the output of nested loops with break?
What will this nested loop print? Pay attention to where break is used.
Python
for i in range(3): for j in range(3): if j == 1: break print(f"{i},{j}")
Attempts:
2 left
💡 Hint
Break only stops the inner loop, not the outer loop.
✗ Incorrect
For each i, j loops from 0 but breaks when j is 1, so only j=0 prints for each i.
❓ Predict Output
advanced2:00remaining
What happens if break is missing in this loop?
This code is supposed to stop when i reaches 3. What happens if we remove the break statement?
Python
for i in range(5): if i == 3: pass print(i)
Attempts:
2 left
💡 Hint
Pass does nothing and does not stop the loop.
✗ Incorrect
Without break, the loop runs fully and prints all numbers from 0 to 4.
❓ Predict Output
expert2:00remaining
What is the output of this loop with else and break?
What will this code print? Notice the else block after the loop.
Python
for i in range(3): if i == 5: break else: print('Loop ended normally')
Attempts:
2 left
💡 Hint
The else block runs only if the loop is not stopped by break.
✗ Incorrect
Since i never equals 5, break never runs, so else runs and prints the message.