Switch statements are useless and long, and they can't be used with certain variable types. They never should have been invented in the first place. If statements are shorter, easier, and more efficient.
I personally have never found any use for this stupid statement.
Conclusion: Don't use switch statements unless asked to.
Despite opinions to the contrary, there are not as many cases with only One True Way to do the job as you might think. There are almost always trade-offs.
You write a program for two quite different audiences: the maintenance programmers (ie, anybody else who might read your code) and the machine. Let's look at some ways in which the switch statement can be helpful to each audience.
For maintenance programs (ie, people reading the code, including you) ...
... You only need to write the switch variable once; no risk of mistyping it anywhere in the
if cascade.
... Logical 'OR' (where any of several values lead to the same choice) is much more concise in the switch statement than in an
if cascade.
... The default clause can be used to trap impossible failures. The use of such traps is a hallmark of robust professional sottware. Static analysis tools like lint can warn you if you forgot to put in the default clause.
... When the switch variable is an enumerated type, static analysis tools like flexelint can warn you if you left any of the enumeration values out of the case clauses.
... When reading code casually, it is obvious that all case clauses depend on the single switch variable.
If cascades must be examined more closely to determine that the same variable is tested every time.
For the machine ...
... I've heard rumors that some optimizing compilers turn switch statements into jump tables, although I've never run across one myself. A jump table would be a whole lot faster than an
if cascade.
... Duff's Device!
Don't get me wrong, there are lots of situations where an
if cascade is a better choice than switch. My code contains a lot more if statements than switch statements. I hope I've given you some reason to be a little less dismissive of the switch statement.