switch

Summary

Evaluates an expression, matches its result against one or more case expressions, and executes statements for the corresponding case.

Syntax

switch (expression) {
    case case_expression1:
        statements1;
        break;
    case case_expression2:
        statements2;
        break;
    ...
    case case_expressionN:
        statementsN;
        break;
    [default:
        statements_default;
        break;]
}

Parameters

expression
An expression to evaluate to match its result against possible cases.
case_expressionN
An evaluated expression result used to match against the resulting value of the evaluated expression of the switch.
statementsN
The statements to execute if the result of the evaluated switch expression matches the result of the corresponding case expression.
statements_default
The statements to execute if the result of the evaluated expression does not match any other case expression result.

Description

The body of a switch statement can contain an arbitrary number of case clauses as long as all the case clauses are unique. The default clause is optional, and there can be one default close at most.

First, the expression is evaluated. The result of the evaluated expression is matched against each case clause's expression. If there is a matching case clause, the statements for the corresponding clause will be executed. If none of the case clauses contain a matching result then the statements in the default clause are executed. If there are no matching case clauses and no default clause then the program will continue executing at the first statement following the switch statement.

A break statement should be inserted in each catch clause and the default clause. The break statement will exit the clause and continue program execution at the first statement following the switch statement.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import System;
 
int x = 3;
 
switch (x) {
case 1:
    Console.log("x equals 1");
    break;
case 2:
    Console.log("x equals 2");
    break;
case 3:
    Console.log("x equals 3");
    break;
default:
    Console.log("x does not equal 1, 2, or 3");
    break;
}

The equivalent if-else statement would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import System;
 
int x = 3;
 
if (x === 1) {
    Console.log("x equals 1");
}
else if (x === 2) {
    Console.log("x equals 2");
}
else if (x === 3) {
    Console.log("x equals 3");
}
else {
    Console.log("x does not equal 1, 2, or 3");
}

As the example illustrates, sometimes if/else can become unwieldy and a switch statement might be desired.

All the variables declared inside a switch statement are scoped to the switch statement itself and not to its individual case clauses.

Combining case and default Clauses

case and default clauses can be combined to specify that the same action(s) can occur for all matching cases:

1
2
3
4
5
6
7
8
9
10
11
switch (value) {
case 1:
case 2:
case 3:
    Console.log("Value is 1, 2, or 3.");
    break;
case 4:
default:
    Console.log("Value is 4 or any other number.");
    break;
}

Switch Fallthrough

With rare exceptions, each case and default clause should contain the break keyword. Otherwise, you risk "falling through" to the next case clause. The following example will execute every case clause:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int state = 1;
 
switch (state) {
case 1:
    Console.log("Start State");
    state = 2;
case 2:
    Console.log("State 2");
    state = 3;
case 3:
    Console.log("State 3");
default:
    Console.log("Final State");
}

The compiler will raise a warning at compile time if a case or default clause does not contain a break statement. Unimplemented

Case Equality

Please note that for a switch statement, the equality test for each case is equivalent to the strict equals (===) operator. Therefore, a switch statement testing for the string "1" will not equal the numeric literal 1. In other words, the types of the value and expression result should be equal. For example:

1
2
3
4
5
6
7
8
9
10
11
12
import System;
 
string foo = "1";
 
switch (foo) {
case 1:
    Console.log("This line will never be executed.");
    break;
case "1":
    Console.log("Found a match!");
    break;
}

Furthermore, references are considered equal. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import System;
 
var foo = {};
var bar = foo;
var baz = {};
 
switch (foo) {
case bar:
    Console.log("Found a match!");
    break;
case baz:
    Console.log("This line will never be executed.");
    break;
}

Differences to JavaScript

Unlike JavaScript, the variables declared in a switch statement in JS++ are scoped to the switch statement itself rather than to the nearest function or global scope.

Examples

Days of the Week
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string day;
 
switch (today) {
case 1: day = "Sunday";
        break;
case 2: day = "Monday";
        break;
case 3: day = "Tuesday";
        break;
case 4: day = "Wednesday";
        break;
case 5: day = "Thursday";
        break;
case 6: day = "Friday";
        break;
case 7: day = "Saturday";
        break;
}

See Also

Share

HTML | BBCode | Direct Link