0
0
Pythonprogramming~20 mins

Why loop control is required in Python - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Control Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A0 1 2
B0 1 2 3 4
C0 1 2 3
D1 2 3
Attempts:
2 left
💡 Hint
The break statement stops the loop immediately when the condition is met.
Predict Output
intermediate
2: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)
A0 1 2
B0 1 2 3 4
C2
D0 1 3 4
Attempts:
2 left
💡 Hint
Continue skips the current loop cycle when the condition is true.
Predict Output
advanced
2: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}")
A0,0 1,0 2,0
B0,0 0,1 1,0 1,1 2,0 2,1
C0,0 1,0 2,0 0,1 1,1 2,1
D0,0 1,0 2,0 0,1
Attempts:
2 left
💡 Hint
Break only stops the inner loop, not the outer loop.
Predict Output
advanced
2: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)
A0 1 2
B0 1 2 3 4
C0 1 2 3
D0 1 2 4
Attempts:
2 left
💡 Hint
Pass does nothing and does not stop the loop.
Predict Output
expert
2: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')
ANo output
B0 1 2
CLoop ended normally
D0 1 2 Loop ended normally
Attempts:
2 left
💡 Hint
The else block runs only if the loop is not stopped by break.