Explanation:
-
Initialization:
-
A variable i is set to 5.
-
-
Loop Condition:
-
The loop runs as long as i is greater than 0.
-
-
Decrement i:
-
In each iteration, i is reduced by 1 before any checks or printing.
-
-
Check for break:
-
If i becomes 2, the loop immediately stops (breaks).
-
-
Print i:
-
If i is not 2, it prints the current value of i.
-
๐ Iteration Details:
| Before Loop | i = 5 |
|---|---|
| Iteration 1 | i becomes 4 → i != 2 → print(4) |
| Iteration 2 | i becomes 3 → i != 2 → print(3) |
| Iteration 3 | i becomes 2 → i == 2 → breaks loop |
✅ Output:
Key Concept:
break immediately stops the loop when the condition is met.
i -= 1 happens before the if check and print().


0 Comments:
Post a Comment