Control Structures Switch statements
Control structures: switch statement
Switch statements in JavaScript take the same form as in Java:
switch ( exp r ) {
case expr1 :
stat ement s
break ;
case expr2 :
stat ement s
break ;
def ault :
stat ement s
break ;
}
•
there can be arbitrarily many case-clauses
•
the default-clause is optional but there can be
at most one
•
expr is evaluated only once and then compared
to expr1, expr2, etc using (loose) equality ==
•
once two expressions are found to be equal the
corresponing clause is executed
•
if none of expr1, expr2, etc are equal to expr,
then the default-clause will be executed
•
break ‘breaks out’ of the switch statement
•
if a clause does not contain a break command,
then execution moves to the next clause
COMP519 Web Programming Lecture 12 Slide L12 – 16