HTML and CSS Reference
In-Depth Information
FORMAT
switch (expression){
case label :
statement(s);
break;
case label :
statement(s);
break;
...
default : statement;
}
EXAMPLE
switch (color){
case "red":
alert("Hot!");
break;
case "blue":
alert("Cold.");
break;
default:
alert("Not a good choice.");
break;
}
The value of the
switch
expression is matched against the expressions, called labels,
following the
case
keyword. The
case
labels are constants, either string or numeric. Each
label is terminated with a colon. The default label is optional, but its action is taken if
none of the other cases match the
switch
expression. After a match is found, the state-
ments after the matched label are executed for that case. If none of the cases are
matched, the control drops to the
default
case. The default is optional. If a
break
state-
ment is omitted, all statements below the matched label are executed until either a
break
is reached or the entire
switch
block exits.
EXAMPLE
6.4
<html>
<head>
<title>The Switch Statement</title>
</head>
<body>
Continues