switch Statement
Description
The switch statement provides a way to execute a specific set of program statements based on an expression value. Each set of program statements has a value associated with them. A case statement represents this value. If the switch statement expression matches a case statement, then the logic that is associated with that case statement executes.
When a switch expression-case statement match is found, execution begins at the statement immediately following the case statement. Execution continues through each statement following the case statement until a break statement is encountered. The break statement forces an immediate exit from the switch statement.
When a break statement is encountered, execution immediately jumps to the first statement following the end of the switch statement. The break statement is optional.
If an expression / case statement match is not found, the logic associated with the default case executes. The default case is optional.
The case labels must evaluate as strings.
Syntax
switch (string-expression) { case string1: statement1a; [statement1b; …] [break;] case string2: statement2a; [statement2b; …] [break;] default: default-stmt1; [default-stmt2; …] [break;] }
statement1a, statement1b, statement2a, statement2b, default-stmt1, and defaultstmt2 all represent executable program statements.
switch (user) { case "admin": hostmachine = "AdminHost"; break; case "helpdesk": hostmachine = "HelpDeskHost";break; default: reject; }
For more information, please see if Statement.