Implemented in | Navigator 4.0, Netscape Server 3.0 |
Syntax
switch (expression){
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
} Arguments
expression | Value matched against label. |
label | Identifier used to match against expression. |
statement | Any statement. |
Description
If a match is found, the program executes the associated statement.
The program first looks for a label matching the value of expression and then executes the associated statement. If no matching label is found, the program looks for the optional default statement, and if found, executes the associated statement. If no default statement is found, the program continues execution at the statement following the end of switch
.
The optional break
statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break
is omitted, the program continues execution at the next statement in the switch
statement.
Example
In the following example, if expression
evaluates to "Bananas," the program matches the value with case "Bananas" and executes the associated statement. When break
is encountered, the program breaks out of switch
and executes the statement following switch
. If break
were omitted, the statement for case "Cherries" would also be executed.
Last Updated: 10/31/97 12:29:59