Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
Python - While Loop

What will be the output of this code snippet?

num = 0
while num < 4:
    if num == 2:
        break
    print(num)
    num += 1
else:
    print('Completed')
ACompleted
B0 1 Completed
C0 1
D0 1 2
Step-by-Step Solution
Solution:
  1. Step 1: Loop execution

    The loop prints 0 and 1.
  2. Step 2: Break condition

    When num equals 2, the break statement exits the loop immediately.
  3. Step 3: Else block

    Since the loop was exited by break, the else block does not execute.
  4. Final Answer:

    0 1 -> Option C
  5. Quick Check:

    Break prevents else from running [OK]
Quick Trick: Else skips if loop breaks early [OK]
Common Mistakes:
MISTAKES
  • Expecting else to run after break
  • Printing the break value
  • Confusing break with continue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes