|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Working with LoopsAlthough you can use simple for and while loops for straightforward tasks, there are some considerations you should make when using more complicated loops. In the next sections, we'll look at infinite loops and the break and continue statements, which give you more control over your loops. Creating an Infinite LoopThe for and while loops give you quite a bit of control over the loop. In some cases, this can cause problems if you're not careful. For example, look at the following loop code: while (i < 10) {
n++;
values[n] = 0;
}There's a mistake in this example. The condition of the while loop refers to the i variable, but that variable doesn't actually change during the loop. This creates an infinite loop. The loop will continue executing until the user stops it, or until it generates an error of some kind. Infinite loops can't always be stopped by the user, except by quitting the browserand some loops can even prevent the browser from quitting, or cause a crash. Obviously, infinite loops are something to avoid. They can also be difficult to spot because JavaScript won't give you an error that actually tells you there is an infinite loop. Thus, each time you create a loop in a script, you should be careful to make sure there's a way out. By the Way Depending on the browser version in use, an infinite loop might even make the browser stop responding to the user. Be sure you provide an escape route from infinite loops, and save your script before you test it just in case. Occasionally, you might want to create an infinite loop deliberately. This might include situations when you want your program to execute until the user stops it, or if you are providing an escape route with the break statement, which is introduced in the next section. Here's an easy way to create an infinite loop: while (true) {Because the value true is the conditional, this loop will always find its condition to be true. Escaping from a LoopThere is one way out of an infinite loop. You can use the break statement during a loop to exit it immediately and continue with the first statement after the loop. Here is a simple example of the use of break: while (true) {
n++;
if (values[n] == 1) break;
}Although the while statement is set up as an infinite loop, the if statement checks the corresponding value of an array. If it finds a value of 1, it exits the loop. When the JavaScript interpreter encounters a break statement, it skips the rest of the loop and continues the script with the first statement after the right brace at the loop's end. You can use the break statement in any type of loop, whether infinite or not. This provides an easy way to exit if an error occurs, or if another condition is met. Continuing a LoopOne more statement is available to help you control the execution of statements in a loop. The continue statement skips the rest of the loop but, unlike break, it continues with the next iteration of the loop. Here is a simple example: for (i=1; i<21; i++) {
if (score[i]==0) continue;
document.write("Student number ",i, " Score: ", score[i], "\n");
}This script uses a for loop to print out scores for 20 students, stored in the score array. The if statement is used to check for scores with a value of 0. The script assumes that a score of 0 means that the student didn't take the test, so it continues the loop without printing that score. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |