Infinite loop: Difference between revisions

Content deleted Content added
m External links: Comment add.
Link suggestions feature: 3 links added.
 
(9 intermediate revisions by 9 users not shown)
Line 18:
|url-status=live
}}</ref> is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs, such as turning off power via a switch or pulling a plug. It may be intentional.
 
There is no general algorithm to determine whether a computer program contains an infinite loop or not; this is the [[halting problem]].
 
==Overview==
Line 73 ⟶ 75:
|archive-url=https://web.archive.org/web/20190726232111/https://www.pcmag.com/encyclopedia/term/48051/non-preemptive-multitasking
|url-status=live
}}</ref> 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 a user. [[Busy waiting|Busy wait]] loops are also sometimes called "infinite loops". Infinite loops are one possible cause for a computer [[Hang (computing)|hanging or freezing]]; others include [[Thrashing (computer science)|thrashing]], [[deadlock (computer science)|deadlock]], and [[segmentation fault|access violation]]s.
 
==Intended vs unintended looping==
Line 132 ⟶ 134:
==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>
Here the loop is quite obvious, as the last line unconditionally sends execution back to the first.
 
An example inIn [[Java (programming language)|Java]]:
<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]]
 
An example inIn [[Bourne Again Shell]]:
<syntaxhighlight lang="bash">
for ((;;)); do
Line 181 ⟶ 184:
</syntaxhighlight>
 
An example inIn [[Rust (programming language)|Rust]]:
<syntaxhighlight lang="rust">
loop {
Line 278 ⟶ 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 [[email loop]]. An example of an email loop is if someone receives mail from a no reply inbox, but their auto-response is on. They will reply to the no reply inbox, triggering the "this is a no reply inbox" response. This will be sent to the user, who then sends an auto reply to the no-reply inbox, and so on and so forth.
 
==Pseudo-infinite loops==
Line 304 ⟶ 307:
 
===Infinite recursion===
{{Main|Recursion (computer science)#Infinite recursion}}
Infinite recursion is a special case of an infinite loop that is caused by [[recursion (computer science)|recursion]].
 
Line 343 ⟶ 347:
</syntaxhighlight>
 
The term allegedly received its name from a programmer (whose last name is Alderson) who in 1996<ref>{{cite web
|url=https://www.lee-dohm.com/2013/05/24/alderson-loop
|title=Alderson loop