Are all endless loops bad?

Out of curiosity, are all endless loops bad?

  • What are the bad consequences and consequences that can happen if you start an endless loop?
  • In addition, if they are not all bad, could you give a few examples when they will serve a meaningful purpose?
  • And do they need to have something to close the instance? For example, we always close StreamReader after using it in Java (you don’t know why).
+4
source share
7 answers

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

Infinite loops are often found in many scenarios, mainly in the event loop, where the program sits in an infinite loop and waits for some external event that processes and returns to wait. This is how the GUI and many servers are programmed.

+4
source
  • If you write your program correctly, there are no side effects due to the cycle.
  • on microcontrollers, infinite loops are used to never reach the end of a program. At the end of the microcontroller program, in most cases there is no such thing as an OS that could take over. Then a state is reached in which there is no specific behavior, and the program can do something.
+1
source

No, they are not bad, they are really useful.

It depends on whether you left a piece of code that eats memory when an endless loop continues. Infinite loops are used in almost all cases: video games, networks, machine learning, etc., since infinite loops are usually used for instant user input / events.

+1
source

Take an example of a simple server listening for connections

  • Listen to requests
  • Get a request, create a stream for this request
  • Rinse and repeat

This scenario is quite common; you see it all the time in event processing.

Negative

  • The program will never end unless you kill it manually (also positive, depending on the situation)

Do they need to close something for the instance?

  • No, you, as already mentioned, cannot kill it manually, but you can implement the input command line to accept the kill command to end the grace
0
source

Sometimes you hear a killer phrase loop to refer to a cycle of bad behavior (endlessly or otherwise). It is usually reserved for loops that inadvertently consume a huge amount of processor time, memory, or both. All killer cycles are bad.

Froot loops can also be killers, depending on your diabetic condition.

Mobius lanes are good endless loops.

0
source

The term "infinite loop" is most often used for a loop in a program that would not be interrupted if there were no external actions that could stop it. In most cases, using any interrupt key to send an interrupt signal to the operating system proves that in the end it was not an infinite loop. And often power loss or power outage will do the same. And on some platforms with some operating systems, other interrupt signals may occur and stop the process.

Thus, pseudo-infinite loops are useful for processes that you do not want to terminate, with the exception of some β€œexternal” influence.

0
source

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

0
source

All Articles