The Perils of Infinite Loops
Written on 06/29/12 at 14:07:01 EST by GentleGiant
JavascriptingListing 2.4 A while Loop with an Internal break Statement
var x = 1;
var xoddsum = 0;
var xtmp = 0;
var lastx = 0;

while ( true ) {          // 1: loop forever (well, almost)
    xtmp = xoddsum + x;     // 2: compute a trial sum
    if ( xtmp > 100 )     // 3: if it is too large, then...
         break;          // 4: we are done
    xoddsum += x;          // 5: add x to the running sum xoddsum
    x += 2;               // 6: increment x by 2
}
lastx = x;               // 7: save the final value of x in the variable lastx

Listing 2.4 not only illustrates the use of break, it also shows two other elements worth noting. First, listing 2.4 contains a nested conditional: there is an if statement inside the while block. This sort of construct is extremely common, and many levels of nesting are not at all unusual. Second, this example has another very common but somewhat troublesome feature. Since the while test is always true, there is no way for the while to terminate unless the break statement is executed. In the preceding example, it was quite quickly. Suppose, however, that statement 6 had been incorrectly entered as x -= 2. In this case, xoddsum would be getting constantly smaller and xtmp would never exceed 100. This type of error is known as an infinite loop. Listing 2.3 is not immune either, even though it has a conditional test rather than a blanket true. If the final statement of that example had been mistyped as x--, it would never terminate either.

Naturally, infinite loops must be vigorously avoided. They will only terminate when some kind of internal error happens (such as an arithmetic overflow when something becomes too large) or as a result of user intervention. Unfortunately, there is no foolproof way to write a while statement (or a for statement, as we shall see shortly) that is guaranteed to be correct. JavaScript is no different than any other programming language in this respect. However, the following general principles will reduce the opportunity for error:

Avoid while ( true ) whenever possible
Have at least one way of exiting the loop body
If the while ( true ) construction is used, then the logic that exercises the break statement must be correct. If this logic isn't correct then the loop will never terminate. If you restrict you use of while ( true ) you will have fewer infinite loops. The second suggestion is based on the observation that the more chances there are to exit the loop, the less likely it is that the loop will last forever. Listing 2.5 shows a modified version of listing 2.4 in which we have moved the test on the sum to the while clause itself, and have also added a very paranoid test on the number of times through the loop (the variable loopcount).

Listing 2.5 An Improved Form of Listing 2.4
var x = 1;
var xoddsum = 0;
var lastx = 0;
var loopcount = 0;

while ( ( xoddsum + x ) < 100 ) {          // 1: loop while sum is < 100
    xoddsum += x;                    // 2: add x to the sum xoddsum
    x += 2;                         // 3: increment x by 2
    if ( ++loopcount > 1000 )          // 4: if we're working too late..
         break;                    // 5: quit
}
lastx = x;                         // 6: save the final value of x in lastx

News and Comments Brought to you by: Geeks and Bloggers
The comments are owned by the poster. We aren't responsible for its content.