Java Reference
In-Depth Information
if (number === 4) {
alert("You rolled a four");
} else if (number === 5) {
alert("You rolled a five");
} else if(number === 6){
alert("You rolled a six");
} else {
alert("You rolled a number less than four");
}
The switch operator can be used instead, like so:
switch (number) {
case 4:
alert("You rolled a four");
break;
case 5:
alert("You rolled a five");
break;
case 6:
alert("You rolled a six");
break;
default:
alert("You rolled a number less than four");
break;
}
The value that you are comparing goes in parentheses after the switch operator. A case
keyword is then used for each possible value that can occur ( 4 , 5 , and 6 in the example
above). After each case statement is the code that that needs to be run if that case occurs.
It is important to finish each case block with the break keyword, as this stops any more
of the case blocks being executed. Without a break statement, the program will "fall
through" and continue to evaluate subsequent case blocks. This is sometimes implemen-
ted on purpose, but it is confusing and should be avoided―a ninja always finishes a case
block with a break !
Search WWH ::




Custom Search