Infinite loop

There is no general algorithm to determine whether a computer program contains an infinite loop or not; this is the halting problem.

This differs from "a type of computer program that runs the same instructions continuously until it is either stopped or interrupted".

With the now-prevalent preemptive multitasking model, infinite loops usually cause the program to consume all available processor time, but can usually be terminated by a user.

Infinite loops are one possible cause for a computer hanging or freezing; others include thrashing, deadlock, and access violations.

Modern interactive computers require that the computer constantly be monitoring for user input or device activity, so at some fundamental level there is an infinite processing idle loop that must continue until the device is turned off or reset.

Modern computers also typically do not halt the processor or motherboard circuit-driving clocks when they crash.

Instead they fall back to an error condition displaying messages to the operator (such as the blue screen of death), and enter an infinite loop waiting for the user to either respond to a prompt to continue, or reset the device.

Spinlocks are low-level synchronization mechanisms used in concurrent programming to protect shared resources.

This intentional infinite looping is a deliberate design choice aimed at minimizing the time a thread spends waiting for the lock and avoiding the overhead of higher level synchronisation mechanisms such as mutexes.

This makes part of the data structure into a ring, causing naive code to loop forever.

A simple example (in C): The form for (;;) for an infinite loop is traditional, appearing in the standard reference The C Programming Language, and is often punningly pronounced "forever".

In some languages, programmer confusion about mathematical symbols may lead to an unintentional infinite loop.

Another way to fix this particular example would be to use an integer as a loop index, counting the number of iterations that have been performed.

A similar problem occurs frequently in numerical analysis: in order to compute a certain result, an iteration is intended to be carried out until the error is smaller than a chosen tolerance.

An example in bash: An example for loop in C: It appears that this will go on indefinitely, but in fact the value of i will eventually reach the maximum value storable in an unsigned int and adding 1 to that number will wrap-around to 0, breaking the loop.

A C-like pseudocode example of an Alderson loop, where the program is supposed to sum numbers given by the user until zero is given, but where the wrong operator is used: The term allegedly received its name from a programmer (whose last name is Alderson) who in 1996[12] had coded a modal dialog box in Microsoft Access without either an OK or Cancel button, thereby disabling the entire program whenever the box came up.

A blue screen of death on Windows XP . "The device driver got stuck in an infinite loop."