|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Using Multiple Conditions with switchIn the previous example, you used several if statements in a row to test for different conditions. Here is another example of this technique: if (button=="next") window.location="next.html"; else if (button=="previous") window.location="prev.html"; else if (button=="home") window.location="home.html"; else if (button=="back") window.location="menu.html"; Although this is a compact way of doing things, this method can get messy if each if statement has its own block of code with several statements. As an alternative, JavaScript includes the switch statement, which enables you to combine several tests of the same variable or expression into a single block of statements. The following shows the same example converted to use switch: switch(button) {
case "next":
window.location="next.html";
break;
case "previous":
window.location="prev.html";
break;
case "home":
window.location="home.html";
break;
case "back":
window.location="menu.html";
break;
default:
window.alert("Wrong button.");
}The switch statement has several components:
By the Way You can use multiple statements after each case statement within the switch structure. You don't need to enclose them in braces. If the case matches, the JavaScript interpreter executes statements until it encounters a break or the next case. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |