How can I avoid making an infinite loop?
Any time you write a
while loop, you should ensure that the condition you are checking eventually becomes
This can be difficult depending on what your condition is, because it can be hard to know all of the cases under which your program could be executed. For example, if we have a function being called in our condition, and that function has special cases that can be tricky to handle, we might not see the reason for our infinite loop so easily.
For example, say we have a program that has a function named
True if the given number is a valid temperature on Earth and
False otherwise. If we use this function in our
while condition, we must take into consideration how that function calculates its
False output, which is a bit more complicated than just checking a
count being less than some value.