ExamVeda
Login
Home
Introduction to Python
?

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)

Answer & Solution
Correct Answer: Option C
The code initializes the variable 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.
Examveda
Question posted by Examveda
Community

Join the Discussion

No comments yet Be the first to discuss this question.