Define Infinite Loop
In computer programming, a loop is a series of instructions a computer executes a fixed number of times. An infinite loop is a loop that can run forever unless some external event causes the loop to terminate. Fortunately, most infinite loops do end. However, when they don't, problems can occur and adversely affect an application or the computer that hosts it.
-
Trivia
-
"Infinite Loop" is a street that surrounds Apple Computer's corporate office in Cupertino, California. Each of the six buildings that make up the complex has a single-digit address. This unique combination of the computer term "Infinite Loop" and the single-digit address structure used by the company gives Apple an official address of "1 Infinite Loop." The phrase "Infinite Loop" is also the title of a book by Michael Malone that chronicles the beginnings of Apple Computer.
Misconceptions
-
Many infinite loops are not really infinite. At some point, a process will bring them to a halt. That process could be the shutting down of a browser or even the rebooting of a computer. Programmers sometimes refer to non-infinite loops as infinite. One example of a finite infinite loop is a Web page animation. Using JavaScript, a developer might create a loop that moves an object every few seconds. The loop would execute repeatedly. However, it could eventually terminate if the developer decides to code the program so the animation stops when the object reaches a pre-defined point on the screen.
-
Coding
-
The following statement illustrates how a developer creates a regular loop in a program:
loopCounter = 0;
loopLimit = 100;while loopCounter is less than loopLimit
begin;
(do something);
(add one to loopCounter)
end;This code says, "While the loopCounter (0) is less than the loopLimit (100), do something." The loop will repeat 100 times. To create an infinite loop, a developer makes sure the value of loopCounter never reaches 100. He could do that by excluding the "(add one to loopCounter)" statement or by replacing the "while loopCounter is less than loopLimit" statement with "while 1 equals 1." If a developer does that, the loop will never end because one will always equal one.
Examples
-
Timers and clocks on some Web pages sometimes run inside infinite loops. The loop begins when a visitor opens a page and it continues until the visitor leaves the page. Some monitoring applications rely on infinite loop processing to keep an eye on local or remote processes. For example, you could create a Windows Service application that retrieves data from a Web service every hour and stores it in a database.
Warnings
-
Infinite loops are not hard to create. In fact, some developers create them unintentionally. When this happens, desktop and Web-based applications can loop continuously, consuming large amounts of system resources. The following is an example of a common non-infinite loop defined within a JavaScript function:
var loopLimit = 100;
for (var i=0; i< loopLimit; i++) {
// do something
}This loop will "do something" until it reaches the limit set by the variable, "loopLimit." In this example, the loop will run 100 times. However, what happens if the value of loopLimit gets incremented by some other function? If that occurs, the loop might never end. Developers should be aware of this common programming error. Other unintentional loops can occur if a developer relies on some external event, such as a mouse click, to stop the loop and that event fails to happen.
-
References
- Photo Credit kind am laptop image by Yvonne Bogdanski from Fotolia.com