0
While not breaking
Iâm running the below code to print odd numbers. But when the number is my second if statement (breaking condition) is not odd the program keep running indefinitely, it only breaks when the second if statement value is also an odd number. Can someone know the reason why? i = 0 while True: i = i +1 if i % 2 == 0: print("Skipping %d" %i) continue if i == 7: print("Breaking") break print(i) print("Finished")
1 Answer
+ 3
If the number is even, "if i % 2 == 0" is true and the continue statement makes it jump back to the beginning of the while loop, so the break statement won't be executed.