Bird
0
0

Consider this code:

hard📝 Application Q15 of 15
Python - While Loop

Consider this code:

n = 5
while n > 0:
    if n == 3:
        break
    print(n)
    n -= 1
else:
    print('Loop completed')

What will be the output and why?

A5 4 Because break stops loop, else does not run.
B5 4 3 Loop completed Because else always runs.
C5 4 3 Because break is inside if, else runs anyway.
DLoop completed Because else runs before loop.
Step-by-Step Solution
Solution:
  1. Step 1: Trace loop iterations and break

    n starts at 5, prints 5 and 4. When n == 3, break stops the loop immediately.
  2. Step 2: Understand else block behavior

    Because the loop was stopped by break, the else block does not run, so 'Loop completed' is not printed.
  3. Final Answer:

    5 4 Because break stops loop, else does not run. -> Option A
  4. Quick Check:

    Break skips else, so only 5 and 4 print [OK]
Quick Trick: Break skips else; else runs only if no break [OK]
Common Mistakes:
MISTAKES
  • Assuming else runs even after break
  • Printing 3 inside loop
  • Thinking else runs before loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes