Why Switch/Case and not If/Else If?
This question in mainly pointed at C/C++, but I guess other languages are relevant as well.
I can't understand why is switch/case still being used instead of if/else if. It seems to me much like using goto's, and results in the same sort of messy code, while the same results could be acheived with if/else if's in a much more organized manner.
Still, I see these blocks around quite often. A common place to find them is near a message-loop (WndProc...), whereas these are among the places when they raise the heaviest havoc: variables are shared along the entire block, even when not propriate (and can't be initialized inside it). Extra attention has to be put on not dropping break's, and so on...
Personally, I avoid using them, and I wonder wether I'm missing something?
Are they more efficient than if/else's? Are they carried on by tradition?
Solution 1:
Summarising my initial post and comments - there are several advantages of switch
statement over if
/else
statement:
Cleaner code. Code with multiple chained
if
/else if ...
looks messy and is difficult to maintain -switch
gives cleaner structure.Performance. For dense
case
values compiler generates jump table, for sparse - binary search or series ofif
/else
, so in worst caseswitch
is as fast asif
/else
, but typically faster. Although some compilers can similarly optimiseif
/else
.Test order doesn't matter. To speed up series of
if
/else
tests one needs to put more likely cases first. Withswitch
/case
programmer doesn't need to think about this.Default can be anywhere. With
if
/else
default case must be at the very end - after lastelse
. Inswitch
-default
can be anywhere, wherever programmer finds it more appropriate.Common code. If you need to execute common code for several cases, you may omit
break
and the execution will "fall through" - something you cannot achieve withif
/else
. (There is a good practice to place a special comment/* FALLTHROUGH */
for such cases - lint recognises it and doesn't complain, without this comment it does complain as it is common error to forgotbreak
).
Thanks to all commenters.
Solution 2:
Well, one reason is clarity....
if you have a switch/case, then the expression can't change.... i.e.
switch (foo[bar][baz]) {
case 'a':
...
break;
case 'b':
...
break;
}
whereas with if/else, if you write by mistake (or intent):
if (foo[bar][baz] == 'a') {
....
}
else if (foo[bar][baz+1] == 'b') {
....
}
people reading your code will wonder "were the foo expressions supposed to be the same", or "why are they different"?
Solution 3:
please remember that case/select provides additional flexibility:
- condition is evaluated once
- is flexible enough to build things like the Duff's device
- fallthrough (aka case without break)
as well as it executes much faster (via jump/lookup table) * historically
Solution 4:
Also remember that switch statements allows the flow of control to continue, which allows you to nicely combine conditions while allowing you to add additional code for certain conditions, such as in the following piece of code:
switch (dayOfWeek)
{
case MONDAY:
garfieldUnhappy = true;
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
weekDay = true;
break;
case SATURDAY:
weekendJustStarted = true;
case SUNDAY:
weekendDay = true;
break;
}
Using if/else
statements here instead would not be anywhere as nice.
if (dayOfWeek == MONDAY)
{
garfieldUnhappy = true;
}
if (dayOfWeek == SATURDAY)
{
weekendJustStarted = true;
}
if (dayOfWeek == MONDAY || dayOfWeek == TUESDAY || dayOfWeek == WEDNESDAY
|| dayOfWeek == THURSDAY || dayOfWeek == FRIDAY)
{
weekDay = true;
}
else if (dayOfWeek == SATURDAY || dayOfWeek == SUNDAY)
{
weekendDay = true;
}