?
What will be the output of the following Python program?
What will be the output of the following Python program?
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
Answer & Solution
Correct Answer:
Option
C
The code initializes the variable
Then, it enters a
Inside the loop, it prints the current value of
It then increments
If
Otherwise, the loop continues until
Since the loop is exited via the
Therefore, the output will be: 0 1 2.
i to 0.Then, it enters a
while loop that continues as long as i is less than 5.Inside the loop, it prints the current value of
i.It then increments
i by 1.If
i becomes equal to 3, the if condition is met, and the break statement is executed, exiting the loop.Otherwise, the loop continues until
i reaches 5.Since the loop is exited via the
break statement and not by reaching the end condition, the else block associated with the loop is not executed.Therefore, the output will be: 0 1 2.
Join the Discussion
Login to post a comment or share your explanation.
Login