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!
==Examples of unintentional infinite loops== ===Mathematical errors=== Here is one example of an infinite loop in [[Visual Basic]]: <syntaxhighlight lang=vbnet> dim x as integer do while x < 5 x = 1 x = x + 1 loop </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 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 mathematical symbols may lead to an unintentional infinite loop. For example, here is a snippet in [[C (programming language)|C]]: <syntaxhighlight lang=c> #include <stdio.h> int main(void) { int a = 0; while (a < 10) { printf("%d\n", a); if (a = 5) printf("a equals 5!\n"); a++; } return 0; } </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, the = (assignment) operator was confused with the == (equality test) operator. Instead, this will assign the value of 5 to <code>a</code> at this point in the program. Thus, <code>a</code> will never be able to advance to 10, and this loop cannot terminate. ===Rounding errors=== {| style="float:right; border: 1px solid grey;" |- | ''C output on an [[AMD Turion]] processor:'' |- |x = 0.10000000149011611938 |- |x = 0.20000000298023223877 |- |x = 0.30000001192092895508 |- |x = 0.40000000596046447754 |- |x = 0.50000000000000000000 |- |x = 0.60000002384185791016 |- |x = 0.70000004768371582031 |- |x = 0.80000007152557373047 |- |x = 0.90000009536743164062 |- |x = 1.00000011920928955078 |- |x = 1.10000014305114746094 |- |x = 1.20000016689300537109 |- | ... |} Unexpected behavior in evaluating the terminating condition can also cause this problem. Here is an example in [[C (programming language)|C]]: <syntaxhighlight lang="c"> float x = 0.1; while (x != 1.1) { printf("x = %22.20f\n", x); x += 0.1; } </syntaxhighlight> On some systems, this loop will execute ten times as expected, but on other systems it will never terminate. The problem is that the loop terminating condition <code>(x != 1.1)</code> tests for exact equality of two [[floating point]] values, and the way floating point values are represented in many computers will make this test fail, because they cannot represent the value 0.1 exactly, thus introducing rounding errors on each increment (cf. box). The same can happen in [[Python (programming language)|Python]]: <syntaxhighlight lang="python"> x = 0.1 while x != 1: print(x) x += 0.1 </syntaxhighlight> Because of the likelihood of tests for equality or not-equality failing unexpectedly, it is safer to use greater-than or less-than tests when dealing with floating-point values. For example, instead of testing whether <code>x</code> equals 1.1, one might test whether <code>(x <= 1.0)</code>, or <code>(x < 1.1)</code>, either of which would be certain to exit after a finite number of iterations. Another way to fix this particular example would be to use an [[integer (computer science)|integer]] as a [[control flow|loop index]], counting the number of iterations that have been performed. A similar problem occurs frequently in [[numerical analysis]]: in order to compute a certain result, an iteration is intended to be carried out until the error is smaller than a chosen tolerance. However, because of rounding errors during the iteration, the specified tolerance can never be reached, resulting in an infinite loop.
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)