Using a switch()… complex statement
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 ) {
case constant-1 : statement-1
case constant-2 : statement-2
…
case constant-n : statement-n
default : statement-default
}
Here, the expression evaluates a single result...