Infinite loop
Infinite loops in computer science are loops that are processed again after each processing if the execution is not interrupted by external influences. External influences are those that are not provided for in the regular course of the program, for example switching off the computer .
programming

Infinite loops can occur during programming due to errors if the termination condition is not defined or cannot occur. Strictly speaking, to distinguish whether a loop criterions has if it never is satisfied or whether this is the case only for certain input parameters. The first case is either chosen deliberately or can be found simply by debugging the program. The second is conceptually similar; the last case is almost always undesirable and can be very difficult to find because it may only occur very rarely. It occurs, for example, if the state of the program at the beginning of the loop does not satisfy the loop invariant .
Incorrect termination conditions often cause unintentional endless loops. Depending on the program, such an error can manifest itself in different ways. If resources - such as main memory , for example - are repeatedly occupied within the loop body and not released again, this generally leads to a memory leak . Infinite loops can also express themselves to the user through simple inactivity of the program ("freezing").
Devices that do not have an orderly shutdown ("shutdown") feature often have at least one endless loop that waits for user input in its basic state. If a computer system offers options for the orderly abortion of a program or sequence, programs can also intentionally have endless loops; This is common in multitasking systems for "event loops" that wait for user input; in a single- tasking system, at most one is working at the same time. Endless loops are then ended using the external abort options.
Examples
- An iteration that does not meet the termination condition.
int number = 0;
while (number < 10) {
// Hier fehlt ein Inkrement der Variable.
// Beispiel: number += 1;
}
or
double number = 0.1;
while (number != 1.0) {
number += 0.1;
// Da 0.1 in binärer Form periodisch ist, wird 1.0 nie exakt erreicht.
}
- A recursion that does not meet any termination condition. In this case the program crashes because of a stack overflow .
void recursion(int number) {
// Hier fehlt eine Abbruchbedingung.
// Beispiel: if (number >= 10) return;
recursion(number + 1);
}
- The simplest case for an infinite loop is a loop condition that is always true. However, if there are one or more
break
statements within the loop , then it is no longer an endless loop.
int number = 0;
while (true) {
// Hier fehlt ein Schleifenabbruch.
// Beispiel: if (number >= 10) break;
number += 1;
}
- A jump instruction that never leaves the program.
int number = 0;
begin:
// Hier fehlt ein bedingter Sprung.
// Beispiel: if (number >= 10) goto end;
number += 1;
goto begin;
end:
Countermeasures
In order to prevent unintentional endless loops in programs, the loop condition can be formally verified (e.g. with the wp calculus ). However, even with small programs, this is a very time-consuming process and generally an unsolvable problem (see holding problem ).
Another method that cannot prevent unintentional endless loops, but which limits it in time, is a so-called watchdog : If a program no longer regularly signals that it is running according to regulations, it can react accordingly (e.g. notify the user or the Exit program).
Other examples
Some geometric figures are possible endless loops , such as the circle or the Möbius strip .
Endless tape goes back to Bernard Cousino .
Acoustic feedback is an endless loop, since the same sound pattern is first emitted by the loudspeakers , picked up again by the microphone , and thus emitted repeatedly.
In glossaries or lexica , an infinite loop is sometimes recursively explained - using the practical example of itself:
- Infinite loop - see infinite loop
In colloquial German , the term endless loop is sometimes used to describe a process in which "the cat bites its tail", for example:
- you end up at the starting point on a hotline after selecting all applicable options (see also Buchbinder Wanninger by Karl Valentin ).
- a discussion "goes in circles" and does not seem to lead to any result in the foreseeable future without additional, new arguments .
- in a “ vicious circle ” the consequence of a problem is also its cause, making it impossible to solve the problem.