I cant understand its result | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I cant understand its result

i = 0 while True: i = i +1 if i == 2: print("Skipping 2") continue if i == 5: print("Breaking") break print(i) print("Finished")

9th Mar 2018, 6:33 PM
Julio Hsu Chu
Julio Hsu Chu - avatar
2 Answers
+ 4
while True: ^That's an infinite loop. It'll keep the loop going endlessly until you break it by setting up some sort of condition inside of the code. In this case, it'll break once variable 'i' reaches a value of 5. At the beginning of the loop, variable 'i' is being increased by 1. It checks if i is equal to 2, if it is then it prints that it's skipping and uses 'continue' which forces the loop to go into the next iteration of itself. If it isn't equal to 2, it then checks if it is equal to 5, and if it is print "breaking" and break out of the loop. If it isn't equal to 2 or 5, then it'll simply print the value of 'i' and go into its next loop iteration. Once the loop is broken, it'll print "Finished" and end the program. :::: OUTPUT ::::: 1 Skipping 2 3 4 Breaking Finished
9th Mar 2018, 6:45 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
@Ace's is helpful!
9th Mar 2018, 8:41 PM
dieserrapha
dieserrapha - avatar