Why doesn’t while True run forever?


#1

Question

Why doesn’t while True run forever?

Answer

If we didn’t increment and check the value of count each time the loop runs, it would indeed run infinitely! In this code snippet, we wanted to enter the while loop unconditionally, which is a common need!
Later you’ll learn about the do-while loop, which is more suited for this kind of use, but for now, just know that you can have a loop that would run until some condition inside the loop is met, and then use break to exit.


FAQ: Learn Python- Loops - Break
#2

5 posts were split to a new topic: Why do we still need “break” to exit?