Content deleted Content added
Link suggestions feature: 3 links added. |
|||
(18 intermediate revisions by 15 users not shown) | |||
Line 1:
{{
{{About-distinguish|the programming term|Infinite Loop (street)|Infinite Loop (book)}}
{{Redirect-distinguish|Endless loop|Endless Loop (album)}}
{{Loop constructs}}<!--
In [[computer programming]], an '''infinite loop''' (or '''endless loop''')<ref>{{cite web
There is no general algorithm to determine whether a computer program contains an infinite loop or not; this is the [[halting problem]].
==Overview==
This differs from "a type of computer program that runs the same instructions continuously until it is either stopped or interrupted".<ref>{{cite news
<syntaxhighlight lang="lua">
how_many = 0
Line 56 ⟶ 57:
==Details==
An ''infinite loop'' is a sequence of instructions in a [[computer program]] which loops endlessly, either due to the [[control flow#Loops|loop]] having no terminating condition,<ref>{{cite magazine
|author=<!-- Unstated -->
|archive-date=July 26, 2019
==Intended vs unintended looping==
Line 82 ⟶ 84:
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,<ref>{{cite web
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.
[[Spinlock|Spinlocks]] are low-level synchronization mechanisms used in concurrent programming to protect shared resources. Unlike traditional locks that put a thread to sleep when it can't acquire the lock, spinlocks repeatedly "spin" in an infinite loop until the lock becomes available. 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 [[Lock (computer science)|mutexes]].
Line 104 ⟶ 106:
[[File:Infinite loop BSOD.jpg|thumb|A [[blue screen of death]] on [[Windows XP]]. "The [[device driver]] got stuck in an infinite loop."]]
Most often, the term is used for those situations when this is not the intended result; that is, when this is a [[software bug|bug]].<ref>{{cite web
One common cause, for example, is that
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 [[undecidable problem|undecidability]] of the [[halting problem]].<ref>{{cite web|url=https://www.geeksforgeeks.org/halting-problem-in-theory-of-computation|title=Halting Problem in Theory of Computation|date=3 October 2018|access-date=22 January 2020|archive-date=9 August 2020|archive-url=https://web.archive.org/web/20200809100104/https://www.geeksforgeeks.org/halting-problem-in-theory-of-computation/|url-status=live}}</ref>
Line 120 ⟶ 122:
==Interruption==
As long as the system is responsive, infinite loops can often be interrupted by sending a signal to the process (such as [[SIGINT (POSIX)|SIGINT]] in Unix), or an [[interrupt]] to the processor, causing the current process to be aborted. This can be done in a [[task manager]], in a terminal with the [[Control-C]] command,<ref>{{cite web
==Language support==
{{see also|Control flow}}
Infinite loops can be implemented using various [[control flow]] constructs. Most commonly, in unstructured programming this is jump back up ([[goto]]), while in [[structured programming]] this is an indefinite loop ([[while loop]]) set to never end, either by omitting the condition or explicitly setting it to true, as <code>while (true) ...</code>.
Some languages have special constructs for infinite loops, typically by omitting the condition from an indefinite loop. Examples include Ada (<code>loop ... end loop</code>),<ref>[[b:Ada Programming/Control#Endless Loop|Ada Programming: Control: Endless Loop]]</ref> Fortran (<code>DO ... END DO</code>), Go (<code>for { ... }</code>), Ruby (<code>loop do ... end</code>), and Rust (<code>loop { ... }</code>).
Line 159 ⟶ 161:
</syntaxhighlight>
A similar example in [[MS-DOS]] compatible batch files:
<syntaxhighlight lang="bat">
:A
Line 165 ⟶ 167:
goto :A
</syntaxhighlight>
<syntaxhighlight lang="java">
while (true) {
Line 174 ⟶ 175:
</syntaxhighlight>
The while loop never terminates because its condition is always true.
An example in [[Bourne Again Shell]]▼
<syntaxhighlight lang="bash">
for ((;;)); do
Line 181 ⟶ 184:
</syntaxhighlight>
<syntaxhighlight lang="rust">
loop {
Line 202 ⟶ 205:
This creates a situation where <code>x</code> will never be greater than 5, since at the start of the loop code, <code>x</code> is assigned the value of 1 (regardless of any previous value) before it is changed to <code>x</code> + 1. Thus the loop will always result in <code>x</code> = 2 and will never break. This could be fixed by moving the <code>x = 1</code> instruction outside the loop so that its initial value is set only once.
In some languages, programmer confusion about
<syntaxhighlight lang=c>
#include <stdio.h>
Line 219 ⟶ 222:
</syntaxhighlight>
The expected output is the numbers 0 through 9, with an interjected "a equals 5!" between 5 and 6. However, in the line "<code>if (a = 5)</code>" above,
===Rounding errors===
{| style="float:right; border: 1px solid grey;"
|-
Line 279 ⟶ 281:
==Multi-party loops==
An infinite loop may be caused by several entities interacting. Consider a server that always replies with an [[error message]] if it does not understand the request. Even if there is no possibility for an infinite loop within the server itself, a system comprising two of them (''A'' and ''B'') may loop endlessly: if ''A'' receives a message of unknown type from ''B'', then ''A'' replies with an error message to ''B''; if ''B'' does not understand the error message, it replies to ''A'' with its own error message; if ''A'' does not understand the error message from ''B'', it sends yet another error message, and so on.
One common example of such situation is an
==Pseudo-infinite loops==
Line 305 ⟶ 307:
===Infinite recursion===
Infinite recursion is a special case of an infinite loop that is caused by [[recursion (computer science)|recursion]].
The following example in [[Visual Basic for Applications
<syntaxhighlight lang="vbscript">
Sub Test1()
Line 326 ⟶ 329:
===Alderson loop===
''Alderson loop'' is a rare slang or [[The Jargon File|jargon]] term for an infinite loop where there is an exit condition available, but inaccessible in
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
<syntaxhighlight lang="C">
int sum = 0;
Line 344 ⟶ 347:
</syntaxhighlight>
The term allegedly received its name from a programmer (whose last name is Alderson) who in 1996<ref>{{cite web
==See also==
{{portal|Mathematics}}
*[[Cycle detection]]
*[[Divergence (computer science)]]
*[[Fork bomb]] (an infinite loop is one of two key components)
*[[Infinite regress]]
▲*[[Recursion (computer science)]]
==References==
{{Reflist}}
==External links==
Line 372:
{{DEFAULTSORT:Infinite Loop}}
[[Category:Control flow]]
[[Category:Iteration in programming]]
[[Category:Programming language comparisons]]
[[Category:Recursion]]
[[Category:Software bugs]]
<!-- Hidden categories below -->
[[Category:Articles with example BASIC code]]
[[Category:Articles with example C code]]
[[Category:Articles with example Java code]]
[[Category:Articles with example PHP code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example Rust code]]
|