Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Infinite loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Pseudo-infinite loops== A pseudo-infinite loop is a loop that appears infinite but is really just a very long loop. ===Very large numbers=== An example in [[Bash (Unix shell)|bash]]: <syntaxhighlight lang="bash"> for x in $(seq 1000000000); do #loop code done </syntaxhighlight> ===Impossible termination condition=== An example [[for loop]] in [[C (programming language)|C]]: <syntaxhighlight lang="c"> unsigned int i; for (i = 1; i != 0; i++) { /* loop code */ } </syntaxhighlight> It appears that this will go on indefinitely, but in fact the value of <code>i</code> will eventually reach the maximum value storable in an <code>unsigned int</code> and adding 1 to that number will wrap-around to 0, breaking the loop. The actual limit of <code>i</code> depends on the details of the system and [[compiler]] used. With [[arbitrary-precision arithmetic]], this loop would continue until the computer's [[memory (computers)|memory]] could no longer hold <code>i</code>. If <code>i</code> was a signed integer, rather than an unsigned integer, overflow would be undefined. In this case, the compiler could optimize the code into an infinite loop. ===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]]. The following example in [[Visual Basic for Applications]] (VBA) returns a [[stack overflow]] error: <syntaxhighlight lang="vbscript"> Sub Test1() Call Test1 End Sub </syntaxhighlight> ===Break statement=== A "<code>while (true)</code>" loop looks infinite at first glance, but there may be a way to escape the loop through a [[break statement]] or [[return statement]]. Example in [[PHP]]: <syntaxhighlight lang="php"> while (true) { if ($foo->bar()) { return; } } </syntaxhighlight> ===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 an implementation of the code, typically due to a programmer 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 wrong operator is used: <syntaxhighlight lang="C"> int sum = 0; int i; 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. Note: ZERO means FALSE, Non-Zero means TRUE. "i * 0" is ZERO (FALSE)! sum += i; // sum never changes because (i * 0) is 0 for any i; it would change 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 } } </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 |author=Lee Dohm |date=May 24, 2013 |access-date=January 22, 2020 |archive-date=June 19, 2020 |archive-url=https://web.archive.org/web/20200619200434/https://www.lee-dohm.com/2013/05/24/alderson-loop/ |url-status=live }}</ref> had coded a [[modal window|modal]] [[dialog box]] in [[Microsoft Access]] without either an OK or Cancel button, thereby disabling the entire program whenever the box came up.<ref>{{Cite web |url=http://www.catb.org/~esr/jargon/html/A/Alderson-loop.html |title=Alderson Loop |website=[[The Jargon File]], Version 4.4.7 |url-status=live |archive-url=https://web.archive.org/web/20060515053043/http://www.catb.org/~esr/jargon/html/A/Alderson-loop.html |archive-date=2006-05-15 |access-date=2006-05-21}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)