# Infinite loop

(Redirected from Infinite recursion)

An infinite loop (also known as an endless loop or unproductive loop) is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over. In older operating systems with cooperative multitasking, infinite loops normally caused the entire system to become unresponsive. 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 the user. Busy wait loops are also sometimes called "infinite loops". One possible cause of a computer "freezing" is an infinite loop; others include deadlock and access violations.

## Intended vs unintended looping

Looping is repeating a set of instructions until a specific condition is met. An infinite loop occurs when the condition will never be met, due to some inherent characteristic of the loop.

### Intentional looping

There are a few situations when this is desired behavior. For example, the games on cartridge-based game consoles typically have no exit condition in their main loop, as there is no operating system for the program to exit to; the loop runs until the console is powered off.

Antique punch card-reading unit record equipment would literally halt once a card processing task was completed, since there was no need for the hardware to continue operating, until a new stack of program cards were loaded.

By contrast, 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. In the Apollo Guidance Computer, for example, this outer loop was contained in the Exec program, and if the computer had absolutely no other work to do it would loop running a dummy job that would simply turn off the "computer activity" indicator light.

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, and enter an infinite loop waiting for the user to either respond to a prompt to continue, or to reset the device.

### Unintentional looping

Most often, the term is used for those situations when this is not the intended result; that is, when this is a bug. Such errors are most common among novice programmers, but can be made by experienced programmers as well, because their causes can be quite subtle.

One common cause, for example, is that the programmer intends to iterate over a collection of items such as a linked list, executing the loop code once for each item. Improperly formed links can create a reference loop in the list, where one list element links to one that occurred earlier in the list. This joins part of the list into a circle, causing the program to loop forever.

While most infinite loops can be found by close inspection of the code, there is no general method to determine whether a given program will ever halt or will run forever; this is the undecidability of the halting problem.

## Examples

The simplest example (in C):

#include <stdio.h>
int main()
{
while(1) {
printf("Infinite Loop\n");
}
}


This is a loop that will print "Infinite Loop" without halting.

A similar example in first generation BASIC :

10 PRINT "INFINITE LOOP"
20 GOTO 10


A simple example in second generation BASIC:

DO
LOOP UNTIL 0


A similar example in X86 assembly language:

loop:
; Code to loop here
jmp loop


Another example is in DOS

:A
goto :A


Here the loop is quite obvious, as the last line unconditionally sends execution back to the first.

An example in Python

while True:
print("Infinite Loop")


An example in Bash

return;
}
}


### Alderson loop

An Alderson loop is a slang or jargon term for an infinite loop where there is an exit condition available, but inaccessible in the current implementation of the code, typically due to programmer's error. These are most common and visible while debugging user interface code.

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 programmer has used the wrong operator:

sum = 0;
while (true) {
printf("Input a number to add to the sum or 0 to quit");
i = getUserInput();
if (i * 0) { // if i times 0 is true, add i to the sum
sum += i; // this never happens because (i * 0) is 0 for any i; it would work if we had != in the condition instead of *
}
if (sum > 100) {
break;    // terminate the loop; exit condition exists but is never reached because sum is never added to
}
}


The term allegedly received its name from a programmer who had coded a modal message box in Microsoft Access without either an OK or Cancel button, thereby disabling the entire program whenever the box came up.[1]