+ 1
Please explain this code
i = 0 while True: i = i +1 if i == 2: print("Skipping 2") continue if i == 5: print("Breaking") break print(i) print("Finished")
2 Answers
+ 3
# Initialization i=0
*The loop starts running as it's a infinite loop because you have written true.
* Increment statement i.e. the value of i will increase by 1 in every turn.
* New value of i after increment =1
* As both the conditions are not satisfied, it will print 1
*Then i = 2
*as the 1st condition is satisfied it will print skipping two and will skip two
* Then i = 3
* It will print 3
* After that it will print 4
* i = 5
* As the 2nd condition is satisfied it will print breaking and will break the loop.
After that it will print finished
Thanks
+ 2
In the loop, you add 1 to i each time it runs.
When i equals 2, it prints "Skipping 2", then breaks that 1 iteration.
When i equals 5, it prints "Breaking", then breaks out of the loop
Result:
1
Skipping 2
3
4
Breaking
Finished