Once in a while you may run into an infinite loop. Sometimes they are necessary and welcomed, but most of the time they are unwanted. In this video you’ll learn what infinite loops are and how they can occur.
So, one thing you may have run into while dealing with
while loops is this idea of a loop that doesn’t end—we called them infinite loops. And this happens whenever we don’t change the condition.
So if a condition is always returning
True, then it’s always going to execute. Let’s show an example. In the one that we’ve been using,
n = 5 and we have our
while n > 0: we’re going to execute this code.
01:41 So if I want to break out, I just hit Control + C and it is forced to reset. So it’s going to stop, you can use that tip whenever you need to break out of an infinite loop. Now, infinite loops can be useful in some senses.
If you need something to happen forever and you never want something to stop, then you can write an infinite loop. But normally, whenever we are writing our
while loops, we always want to make sure that we, at some point, are changing our variable or making sure that our condition becomes
False at one point or another, or else we will run into an infinite loop.
Become a Member to join the conversation.