0
While loop - beginner
Please help me throughout this code asap . As I am a beginner , please help me to understand 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")
1 Réponse
+ 1
If you haven't gone through the comments of that module. Then here's one---
we start with i = 0
while True - so it will always run
then we add 1 to 0 (i = i + 1) and our i = 1 now, and we go to the next step - if i == 2. it's False, so it's skipped and we go to if i == 5, False again, so we just do print(i)
next time it runs from the beginning, but our i is 1 now, it adds another 1 and we get 2 because i= i(1) + 1
here we have True for i == 2 so it prints "skipping 2".
and by "continue" we make it stop running and return to the beginning, where we add another 1 to the i (otherwise it'd print "2" at the end running "print(i)" thing before doing the next step)