Content deleted Content added
HTML5, CSS3! (talk | contribs) m Added JS Tags: Reverted Visual edit |
Link suggestions feature: 3 links added. |
||
(35 intermediate revisions by 25 users not shown) | |||
Line 1:
{{
{{About-distinguish|the programming
{{Redirect-distinguish|Endless loop
{{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==
▲* "a type of computer program that runs the same instructions continuously until it is either stopped or interrupted."<ref>{{cite news
▲ |newspaper=[[The New York Times]]
▲ |url=https://archive.nytimes.com/www.nytimes.com/library/tech/99/08/biztech/articles/16digi.html
|last=Caruso |first=Denise
▲ |title=Overload of Hangers-On Creates Bumpy Ride for Internet Stocks
|archive-date=December 27, 2019▼
▲ |archive-url=https://web.archive.org/web/20191227145519/https://archive.nytimes.com/www.nytimes.com/library/tech/99/08/biztech/articles/16digi.html
}}</ref> Consider the following [[pseudocode]]:▼
▲ |url-status=live
▲Consider the following [[pseudocode]]:
<syntaxhighlight lang="lua">
how_many = 0
Line 59 ⟶ 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 -->
▲ |access-date=August 15, 2015
==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
===Intentional looping===
Line 85 ⟶ 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
==== Spinlocks ====
[[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]].
====Multi-threading====
In multi-threaded programs some threads can be executing inside infinite loops without causing the entire program to be stuck in an infinite loop. If the main thread exits, all threads of the process are forcefully stopped, thus all execution ends and the process/program terminates. The threads inside the infinite loops can perform "housekeeping" tasks or they can be in a blocked state waiting for input (from socket/queue) and resume execution every time input is received.
===Unintentional looping===
[[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
==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 138 ⟶ 141:
A simple example (in [[C (programming language)|C]]):
<syntaxhighlight lang="c
#include <stdio.h>
Line 144 ⟶ 147:
{
for (;;) // or equivalently, while (1)
printf("Infinite Loop\n");
return 0;
}
</syntaxhighlight>
The form <code>for (;;)</code> for an infinite loop is traditional, appearing in the standard reference ''[[The C Programming Language]]'', and is often punningly pronounced "forever".<ref>{{Cite web |url=https://stackoverflow.com/questions/20186809/endless-loop-in-c-c |title=Endless loop in C/C++ |url-status=live |archive-url=https://web.archive.org/web/20160803202212/http://stackoverflow.com/questions/20186809/endless-loop-in-c-c |archive-date=2016-08-03}}</ref>
This is a loop that will print "Infinite Loop" without halting.
A similar example in 1980s-era [[BASIC programming language|BASIC]]:
<syntaxhighlight lang="
10 PRINT "INFINITE LOOP"
20 GOTO 10
</syntaxhighlight>
A similar example in [[MS-DOS]] compatible batch files:
<syntaxhighlight lang="bat">
:A
Line 164 ⟶ 167:
goto :A
</syntaxhighlight>
<syntaxhighlight lang="java">
while (true) {
Line 173 ⟶ 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 180 ⟶ 184:
</syntaxhighlight>
<syntaxhighlight lang="rust">
loop {
println!("Infinite loop");
}
</syntaxhighlight>
Line 195:
===Mathematical errors===
Here is one example of an infinite loop in [[Visual Basic]]:
<syntaxhighlight lang=
dim x as integer
do while x < 5
Line 203:
</syntaxhighlight>
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
In some languages, programmer confusion about
<syntaxhighlight lang=c>
#include <stdio.h>
Line 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 282 ⟶ 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 296 ⟶ 295:
done
</syntaxhighlight>
===Impossible termination condition===
An example [[for loop]] in [[C (programming language)|C]]:
Line 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="
Sub Test1()
Call Test1
Line 328 ⟶ 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 346 ⟶ 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 374 ⟶ 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]]
|