Examveda

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)

A. error

B. 0 1 2 0

C. 0 1 2

D. none of the mentioned

Answer: Option C

Solution (By Examveda Team)

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.

This Question Belongs to Python Program >> Introduction To Python

Join The Discussion

Related Questions on Introduction to Python