Utilizing a
switch conditional statement instead of an
if statement can sometimes present clearer code logic. When we have a variable or an expression containing variables that may have different resulting values followed by different actions, it is a good opportunity to use
switch
.
switch (<expression>) {
case <result 1>:
<action 1>;
break;
case <result 2>:
<action 2>;
break;
......
case <result n>:
<action n>;
break;
default:
<other action>;
break;
}
A simple application of a switch ...