Transfer statements such as break and continue are
convenience statements that provide a way to jump out of a loop or skip to the
end of a loop block. Transfer statements are used to simplify complex boolean
logic in loop and switch statements. Here are some simple examples:
It often simplifies coding a loop to be able to break out of the loop without
setting variables to special values or writing complex expressions to test for
many special conditions. The break statement does exactly this.
In the example below the loop will terminate after the counter reaches the value
6.
counter = 0;
while (true) { //potentially infinite loop
if (counter > 5) break
counter++
}
The break statement is particularly useful when you are using
a for loop to search for something. If you find it, there may be no point in
continuing the loop. This may be true when searching through a string, an array,
or object. Here is a somewhat naive script that searches for the lowest prime
factor of an number. In the example the % operator is used to see
if the remainder of dividing the number by i is zero. If it is then i
is a prime factor and the first time this occurs it will be the lowest prime
factor. The loop starts at 2 because we are looking for factors other than 1
and the number itself.
Note that in the example the first factor of 1001 is 7 (143 × 7 = 1001). So
there is no point in continuing as this would mean needlessly looping through
the values 8 through 1000. Of course there is always the possibility that the
number 1001 was prime. If the number is prime the loop will continue with i
will going through all the values less than 1001. If that happens then after
the last time the loop body is executed i will be incremented and i <
aNumber will not be true and the loop will be over. Unfortunately,
while this illustrates how break works and what happens to i
if the break statement is executed or not, it is not a good way to determine
if the break statement was executed. This does not mean that it does not work
but rather that it may introduce problems/bugs if the script is modified. Here
is a better way that is less prone to problems.
In the preceding example, i is not used to determine if a prime
factor was found and the loop broken out of. Instead, the variable isPrime
is set to true before the loop is entered. If a prime factor is
found isPrime is set to false before breaking out
of the loop. If a prime factor is not found then isPrime is still
true after the loop is completed. At the cost in memory of creating
another variable, this makes it easier to understand what the script does and
makes it easier to modify the script without intoducing bugs if the the loop
is changed. For example, there is no point in testing values greater than the
square root of aNumber. If the loop was modified to exit after
reaching the square root of aNumber the test if (i == aNumber)
or even if (i >= aNumber) would no longer work and would have
to be modified. No changes would be required in the preceding example where
the extra isPrime variable was used.
Another statement that may simplify writing a loop allows you to skip over
the remaining statements in a loop and move on to the next iteration is the
continue statement.
counter = 0;
while (counter < 6) {
counter++
if (counter % 2 == 0) continue
document.write("Counter is now: " + counter)
}
This is particularly useful in a for loop when processing or searching
through data when it is necessary to skip over certain exceptional data elements
that should not be processed. In the preceding example only odd numbers would
be written into the document as the document.write statement will be skipped
for even numbers.