Switch-case syntax

The if-else statement and the condition operator, are often used for a two-way branch. If you want to set up a multi-way branch in the program, you can work with switch-case statement.

A switch statement controls the value of an expression of a certain type. Depending on the value of the expression, a certain case statement is executed. If the value of the expression does not match any of these cases, the default statement is executed.

switch (expression) {
    case value1:
        // statements
        break; // optional
    case value2:
        // statements
        break; // optional
    default:   // statements
}