Description:
Executes one of several groups of statements, depending on the value of an expression.
Syntax:
switch (test_expression)
{
case value :
statements
break;
...
default:
statements
break;
}
test_expression - Number or text string.
statements - One or more statements executed if test_expression matches of value.
default - (optional) One or more statements executed if test_expression doesn't match any of the case clauses.
Note:
If
value matches any
case value, then the statements following after the
case are executed up to the
break.
If the statement
break is not present, then the process continues to the following
case sections - this way it is possible, for example, to merge multiple
case sections - see
Example1.
Default is used for processing unexpected values of
value.
In the VBScript language is used the statement for this purpose
Select Case.
Example1:
One portion of statements may belong to multiple values (case 1: and case 2:):
Select and copy to clipboard
var sColor;
var nVar = 1;
switch (nVar)
{
case 1:
case 2:
sColor = "green";
break;
case 3:
sColor = "blue";
break;
default:
sColor = "black";
break;
}
Example2:
Statements can be written on a single row:
switch (nVar)
{
case 1:
case 2: sColor = "green"; break;
case 3: sColor = "blue"; break;
default: sColor = "black"; break;
}