LearnFrenzy
Sign in Take a free test

Loops · Predict the Output · Question 11

while with an else clause Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following Python program?

main.py
i = 0
while i < 3:
    i += 1
else:
    print("done", i)
j = 0
while j < 3:
    j += 1
    if j == 2:
        break
else:
    print("done", j)
print("end", j)
  • A done 3 done 2 end 2
  • B end 2
  • C done 3 end 3
  • D done 3 end 2 Correct
Correct Answer: Option D

The first loop ends because its condition becomes false, so else runs. The second ends with break, so its else is skipped.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links