With the if()… else… statement, the conditional expression evaluates to only one of two values—true or false. But what if we had a single result that could have multiple values, with each value requiring a different bit of code execution?
For this, we have the switch()… statement. This statement evaluates an expression to a single result and selects a pathway where the result matches the known values that we care about.
The syntax of the switch()… statement is as follows:
switch(expression) {
caseconstant-1 :statement-1
caseconstant-2 :statement-2
…
caseconstant-n :statement-n
default : statement-default
}
Here, the expression evaluates to a single result. The next part of the statement is called the case-statement block, which contains one or more case clauses and an optional default: clause. In the case-statement block, the result is compared to each of...