Michael A. Covington said:
I think they followed C too closely. C, in turn, was designed to be
easy to compile.
It's also because a 'switch' can be optimized a lot better. You are
checking a single value against a series of constants, and code can be
generated to do that which is faster than the code for the corresponding
'if' statements. For instance, the value being checked can be loaded into a
register once, and then checked against the constants, which can be
immediate data in the opcodes.
One could write a compiler to detect that the bunch of 'if's are all
checking a single value against a series of constants, and maybe some do
that (probably C compilers, if any), and thus get code as good as a
'switch'.
Also, when a 'switch' uses continuous case values (no gaps), you can build
a jump table, which is even better. Lots of C compilers do this.
Lexers typically rely on such switch statement optimizations for speed when
they handle the next character in the input stream, as an example where it
pays off. Of course, the compiler doesn't know you're compiling a lexer, it
just generates better code for switches, either way.