switch statements

Instead of using multiple if…else.if statements, there is an alternative – the switch statement. The switch statement is more efficient.

When writing a switch statement, you give an expression to evaluate and different statements to be executed, based on the value of the expression. Each case is checked against the value of the expression, until a match is found. If no match is found, a default condition is used.

syntax

switch (expression) {
case condition 1: statement
break;

case condition 2: statement
break;

case condition 3: statement
break;

case condition 4: statement
break;

default: statement
}

The break statement indicates the end of a case. If there was no break statements, then the interpreter would continue to execute each case.