Last update: Sun, Feb 26, 2012 at 9:17 AM.
What is an infinite loop?
  • A picture named tales.gifI've been using the term infinite loop in my docs lately, because there was a problem with one of the features I added that could cause one. But I never explained what it is, and why it's a concern.
  • Programs have a flow to them -- I'm talking about conventional languages, like the one built into the OPML Editor, and JavaScript, and most of the other languages you've heard of. There are some experiments in language (that's how I think of them) that don't flow, and I don't know if they have the possibility of infinite loops. But all conventional languages have the problem.
  • There are two major forms of flow, logic and looping. Logic means going in one direction if something is true, and going the other if something is false. There are variants on this, Pascal has a "case" statement that allows you to have multiple conditions, each with their own possible direction. Most mature languages have something like this.
  • Then there's looping which says -- do this until some condition is true. Or do this 1000 times, which is really the same thing -- do it until the statement that you haven't done a thousand loops is false. But what happens when there's a flaw in the condition?
    • A commenter points out that the statement could be true. If the first part of the condition were evaluated just before midnight on Tuesday, and the second part, just after.
    • He says it's a nitpick, but I have found bugs in programs that were that odd. The programmer's mind works that way, always trying to find the "edge condition" that makes the code fail. If you're writing realtime software, it's amazing how quickly you find those edge cases.
    • A better example would be Loop until today is Tuesday and today is Saturday.
  • Of course most infinite loops aren't so obviously infinite, or the programmer wouldn't have written them that way. Another way of saying the same thing is that bugs are hard to find, or they wouldn't be bugs.
  • BTW, Apple's snail-mail address, One Infinite Loop, Cupertino CA, is a cute reminder that all programs have bugs, even those created by programming gods.
  • One more thing, there are loops you create that are deliberately infinite. For example, the loop that your computer is sitting in right now, waiting for you to do something, is an infinite loop. That doesn't mean it will run forever, just that it would like to. It will stop running when you turn the device off, or when it runs out of battery, or when the world is destoyed in thermonuclear war.