5

Out of curiousity, are all infinite loops bad?

  • What are the bad effects and consequences that will happen if you run an infinite loop?
  • Also, if they are not all bad, could you please give some examples when they will serve a meaningful purpose?
  • And do they need to have something to close the instance? For example, we always close the StreamReader after using it in Java (not sure why).

7 Answers 7

6

I'm not sure what you mean by "bad".

Infinite loops are common in many scenarios mostly event handler loops where the program sits in an infinite loop and waits for some external event to occur which is handles and goes back to wait. This is the way GUIs and many servers are programmed.

Update They're sufficiently useful to justify a construct just for infinite loops in some languages.

3
  • I have come accross articles that spoke of overloading the computer memory when it is inside an infinite loop. Why do the GUI's and servers not overload the memory?
    – Theone
    Jun 29, 2012 at 6:27
  • I don't know what these articles meant by overload. An infinite loop with just a single sleep statement inside it will not affect the memory of a machine. Jun 29, 2012 at 6:30
  • 3
    Memory overloading may happen when there is a memory leak inside of an endless loop. Jun 29, 2012 at 6:32
1
  • If you write your program correctly, there are no side effects that just happen because of the loop.
  • on microcontrollers endless loops are used to never reach the end of the program. At the end of a microcontroller program there's most of the time no such thing as an OS that could take over. Then the state is reached, where no defined behaviour exists and the program may do anything.
1

No, they're not bad, they're actually useful.

It depends whether you left some part of the code that eats up memory as the infinite loop proceeds. Infinite loops are used at almost everything: video games, networking, machine learning, etc. because infinite loops are usually used for getting instantaneous user input/events.

3
  • The loops you are describing may have not-so-obvious ending conditions but they are not infinite.
    – Holger
    Jul 29, 2014 at 8:21
  • 1
    by the term "infinite" do you mean programs that requires machines to run indefinitely?
    – Shinji
    Jul 29, 2014 at 8:31
  • No, I mean that for example, event processing loops will terminate on certain events like closing the last window or “Quit” menu item selected and so even stand-alone devices like a router, etc. have conditions (like “shutdown” command received via ssh) on which the loop ends. True infinite loops are very uncommon.
    – Holger
    Jul 29, 2014 at 8:46
0

Take the example of a simple server, listening for connections

  • Listen for requests
  • Get Request, Spawn thread for that request
  • Rinse and Repeat

This sort of scenario is pretty common, you see it all the time in event handling.

Negative

  • Program will never terminate, unless you kill it manually (also a positive, depending on the situation)

Do they need to have something to close the instance?

  • No, you don't, as mentioned before you can kill it manually but you could implement an input command line to accept a kill command to end gracefully
2
  • is it an infinite loop if you can kill it externally? ;) Jun 29, 2012 at 6:39
  • 1
    Nothing in programming is infinite my friend ;)
    – Robotnik
    Jun 29, 2012 at 6:43
0

You will sometimes hear the phrase killer loop, to refer to a badly behaving loop (infinite or otherwise). Typically reserved for loops that inadvertently consume massive amounts of CPU time, memory, or both. All killer loops are bad.

Froot Loops can also be killer loops depending on your diabetic status.

Mobius strips are good infinite loops.

0

The term "infinite loop" is most often used for a loop in a program that wouldn't terminate if there were no external actions that could terminate it. In most cases, using whatever the "interrupt key" is to send an interrupt signal to the operating system will prove that it wasn't an infinite loop after all. And often a power surge or power outage will do the same. And on some platforms with some operating systems other interrupt signals may occur and halt the process.

So pseudo-infinite loops are useful for processes you don't want to terminate except by some "outside" influence.

0

In real-time programs, an infinite loop is normal. Take a look at the Arduino IDE - the only two functions exposed are setup() and loop(). It is assumed that loop() will never exit unless the power is removed.

1
  • In fact the loop function exits... but it is called again and again forever.. i.e. the main function is basically something like: void main() { setup(); while(1) loop(); }
    – frarugi87
    Jul 30, 2014 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.