Ok, Ignorant question (Stopping Infinite Loops)


So I've gotten the hang of this so far but when I test my code and accidentally make my loop an infinite loop I don't know how to get out of the console without hitting refresh, Is there some sort of way to end the console from running while I'm in it?


Actually, there is not another way besides refreshing your browser to stop the infinite loop. Additionally, if you're working on your computer and accidentally enact an infinite loop, you'll have to force quit the application or even restart your computer if you're lucky. If you're working with a certain kind of IDE, your computer can possibly crash.


Thank you for the quick reply! Alright, that's good to know!

lesson learned.

check = 0
while check >= 0:
check += 1
print "Check over your code again"

My terrible attempt at humor.


it's okay, i thought it was pretty funny :slight_smile: better than what i could come up with
glad i could help !


These happen to me a lot. One thing you'll find when moving away from a web interface is that you have several ways to catch things. But you sort of need to be vigilant. If a program you expect to take 1 second takes 5 or 10, hit ctrl-c immediately. Unfortunately, it's impossible to avoid the risk of an infinite loop completely, but if you see the signs, you can break off before the computer starts to grind. I think.

@kyleaw, what languages do you program in that crash? That seems like a weakness of the IDE. Or maybe it's a Mac thing?

I know I have problems if I leave too many processes open, but that's my fault.

Debugging an infinite loop is harder, but it's a good idea to put in a lot of print statements. Even print "checkpoint 1"/print "checkpoint 2"/print "checkpoint 3" does the job.

In fact if there's a tricky bit of code, you can maybe run it with debugging text, and the computer will use its processors to print stuff on the screen. This may give you information about smaller bugs, and it may be easier to send signals to disrupt it than if you had a loop sitting there not printing anything.


I haven't personally had any experience with this, i just know it's a possibility from reading a lot of other people's responses to things like these.


I'd think definitely it's a problem with older computers and apps, and it's something to be aware of. Fortunately, though, most of the time a ctrl-c can bail, and people don't write about it because it is a relative non-event. So I think there is some bias towards fear.

There's basic stuff we can do like have a stable version of our web browser and IDE. And for all the grief Windows gets, they have made an effort to make their software respond better to crashes and at least tell you why.

Another thing is to google (your language) debugging detect hang and things like that, for when we want to program away from codecademy. It may require tweaking the terms, but solutions are out there, because the groups that author programing languages don't want programmers seeing blue screens of death and switching to another language as a result. I've found debugging/crash-avoidance tools are even more sophisticated than I thought, even taking into account that there are tools out there more sophisticated than I thought. Sort of a reverse Hofstadter's Law.

I don't have all the answers here, but I do want to at least say, hey, here's what to look for and even to expect, to help you and give you less to fear.


As long as there's resources for whatever is required to kill the process you're fine.
So getting anywhere near full memory would start impacting that, at first because you'd mess with cached data used to speed up access and then because allocating new memory would start getting denied. Using all cpu should be fine because the os schedules cpu time between processes - many processes competing would be a problem though.


Actually, on an offline IDE there is a command to exit the loop. :slight_smile:


Yeah, I actually haven't had any problems with infinite loops messing up anything besides my pride, but my post was really an ignorant one regarding how bad they were. They're obviously not helpful or a good practice, but I was just kind of thinking I knew what I talked about when I should've just answered the question about what to do when you cause an infinite loop in CodeCademy. You're absolutely right though. Thanks! :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.