Challenge - 5 Problems
While Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple while loop
What is the output of this code?
Python
i = 0 while i < 3: print(i) i += 1
Attempts:
2 left
💡 Hint
Remember the loop runs while the condition is true, and stops when it becomes false.
✗ Incorrect
The loop starts with i=0 and runs while i is less than 3. It prints 0, then 1, then 2. When i becomes 3, the condition fails and the loop stops.
❓ Predict Output
intermediate2:00remaining
While loop with break statement
What is the output of this code?
Python
count = 0 while True: print(count) count += 1 if count == 3: break
Attempts:
2 left
💡 Hint
The break stops the loop when count reaches 3.
✗ Incorrect
The loop prints count starting at 0. When count reaches 3, the break stops the loop before printing 3.
❓ Predict Output
advanced2:00remaining
While loop with else clause
What is the output of this code?
Python
i = 0 while i < 3: print(i) i += 1 else: print('Done')
Attempts:
2 left
💡 Hint
The else block runs after the while loop finishes normally.
✗ Incorrect
The loop prints 0, 1, 2. After the loop ends because the condition is false, the else block prints 'Done'.
❓ Predict Output
advanced2:00remaining
While loop with continue statement
What is the output of this code?
Python
i = 0 while i < 5: i += 1 if i == 3: continue print(i)
Attempts:
2 left
💡 Hint
The continue skips printing when i is 3.
✗ Incorrect
The loop increments i first. When i is 3, continue skips the print. So 3 is not printed. Others are printed.
🧠 Conceptual
expert2:00remaining
Understanding infinite loops
Which option will cause an infinite loop when run?
Attempts:
2 left
💡 Hint
Check if the loop variable changes so the condition can become false.
✗ Incorrect
Option B never changes i, so i stays 0 and the condition i != 5 is always true, causing an infinite loop.