In what way? I _usually_ find switch easier to read if there's
more than one option.
Or sometimes if there's only one. I use the if for boolean
conditions: is something true, and the switch when I'm logically
thinking in terms of cases: the user entered 'y', the user
entered something else, for example. (Obviously, it's not a
black and white choice. It's conditionned by your point of
view, which isn't an objective fact.)
The only thing that I don't like about switch is the
indentation. By time you get to the code, you've indented 3
times.. i.e.,
void myfunc(int a) {
switch(a) {
case 0:
// do something
break;
case 1:
// etc.
default:
break;
}
}
Doesn't look so bad here, but with 8 character tabs its a
nightmare.
First, since the case's are keywords controlling flow, at the
same level as the switch, you can (and probably should) indent
them at the same level as the switch. (But standards vary: it's
also reasonable to indent them a half a level. The important
part is that the controlled code be indented one level.) Second,
you really shouldn't be using hard tabs: tell your editor to
insert spaces instead of tabs. And set the indent width to
something less than 8. 4 seems about right, but there's
doubtlessly some margin for personal choice here as well.
The one bad thing about switch is that the case doesn't open a
new scope, so you end up having to use additional braces in each
case, i.e.:
switch ( a ) {
case x:
{
// do something here which needs a local
// variable...
}
break;
// ...
}
In practice, though, this turns out to not be much of a problem.
Just about anything complicated to need a local variable should
probably be put in a separate function anyway.
The other weakness of the switch is that the cases all have to
be integral constant expressions. If you need something like:
switch (inputString) {
case "Yes": case "yes":
// do something...
break;
case "No" : case "no":
// do something else...
break;
}
You'll have to use the if ... else if chain.
Performance wise, though, it depends a lot on the actual code.
More on the compiler.
Apparently (on some/most compilers), switch can be optimized
into a jump table. So if you've got a situation when you're
switching on 0, 1, 2, 3,... then the switch will be fast.
First, I've never heard of a compiler which doesn't do this.
The only thing is that the compiler will only do it if the cases
are "dense". Switch over {0, 1, 2, 3, 4}, and you'll certainly
get the jump table. Switch over {'A', 'a', 'x', 'X', '0'}, and
you'll probably get a hard coded binary decision tree (which is
still faster than any direct recoding of the if ... else if...
pattern).
In theory, at least, the compiler could probably do the same
thing with if ... else if ... It would require more work for
the compiler to detect the case, and I don't think many do.
In practice, of course, the difference probably won't be
measurable.
On the other hand, if one case occurs a lot and the others
don't, it might be better to do if/elif/else.
I've heard about at least one compiler which would do this for
you. Now that most compilers can optimize using profiling
output, I would expect such an obvious optimization to become
frequent. (It still won't necessarily outperform the jump
table, but for sparce switches, starting the binary search at
the most frequent case, rather than in the middle, could be a
win if the frequencies are squewed.)