While not breaking | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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")

20th Dec 2018, 8:01 AM
Joe Manycks
Joe Manycks - avatar
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.
20th Dec 2018, 8:18 AM
Anna
Anna - avatar