|
As seen in the previous example an if...else statement can grow sometimes
very large depending on the number of conditions to compare. JavaScript
provides an alternative to a multitude of if...else conditions. It
proposes a switch statement whose syntax is:
switch(Expression)
{
case FirstCase:
Statement1;
case AnotherCase:
Statement2;
case YetAnother:
Statement3;
}
When this statement is written, the interpreter first
evaluates the Expression to get a value. Then it scans each case
from top down to find which one matches the value of Expression. If
it finds a case that matches the result of Expression,
then it executes the corresponding statement.
When writing the switch
statement, provide an expression that can be appropriately evaluated. It
could be a numeric value, a string, or even an expression; make sure the
interpreter can get a value from it. On the right side of each case
keyword, type a value that is of the same type as the value of the Expression. For example, if the
Expression is a number, make sure each
case is accompanied by a number. Here is an example:
switch(MembershipCategory)
{
case 1:
Fee = 100.00;
case 2:
Fee = 150.50;
case 3:
Fee = 175.00;
}
In JavaScript, the Expression can also be a string. In
this case, make sure that the interpreter can determine it. Then type a
valid and comparable string on the left of each case.
The switch syntax we have considered so far:
switch(Expression)
{
case FirstCase:
Statement1;
case AnotherCase:
Statement2;
case YetAnother:
Statement3;
case OneMoreCase:
Statement4;
}
Here is how this syntax works, the interpreter
compares the Expression and gets a value. Then it compares this
value with FirstCase. If the value of Expression and that of
first case are the same, then the interpreter executes Statement1, Statement2,
Statement3, and all Statementn under it. If the value of
Expression and FirstCase are not the same, then the interpreter
moves down to AnotherCase. If the value of Expression and AnotherCase
match, then it executes Statement2, Statement3, and all Statementn
under it. As you can see already, even after the interpreter finds a case
match of the value of Expression, if that case is not the last one, there
is a chance that the interpreter will executes statements that are not
necessary. To make the interpreter execute only the statement that
corresponds to the case that matches the
value of expression, you must ask the interpreter to stop with that case.
This is done by adding the break
keyword at the end of each case. Therefore, a better syntax of the switch
statement would be:
switch(Expression)
{
case FirstCase:
Statement1;
break;
case AnotherCase:
Statement2;
break;
case YetAnother:
Statement3;
break;
case OneMoreCase:
Statement4;
break;
}
The switch above can include as many cases as
necessary. There is still a chance that none of the case would match the
value of Expression. JavaScript proposes a special case that would include
any value that is not one of the other listed cases. This is done by
including the default case as in the following syntax:
switch(Expression)
{
case FirstCase:
Statement1;
break;
case AnotherCase:
Statement2;
break;
case YetAnother:
Statement3;
break;
case OneMoreCase:
Statement4;
break;
default:
DefaultStatement;
}
If none of the cases
matches the value of Expression, then the interpreter would look
for a default statement. If it finds one,
then it executes it. The default case is
executed if none of the cases can be used.
|