Code Explanation:
1. for Loop Initialization
for i in range(3):
range(3) generates values: 0, 1, 2
The loop variable i takes these values one by one.
2. if Condition
if i == 1:
Checks if the current value of i is equal to 1.
3. break Statement
break
When i becomes 1, break immediately terminates the loop.
Control exits the for loop entirely.
4. print Statement
print(i, end=" ")
Prints the value of i followed by a space.
This line runs only when i is not equal to 1.
5. else Block of the for Loop
else:
print("Done")
The else block of a loop runs only if the loop finishes normally (i.e., without break).
Since break is executed when i == 1, the else block will not execute.
6. Step-by-Step Execution
Iteration i Condition (i == 1) Action
1 0 False Print 0
2 1 True break → exit loop
Loop stops here.
7. Final Output
0

0 Comments:
Post a Comment