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!
===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.
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)