Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 723
OB OB Avatar asked Jun 22 '09 17:06

OB OB


People also ask

What is the advantage of switch case over if-else decision statements?

Advantages of C++ Switch Statement The switch statement has a fixed depth. It allows the best-optimized implementation for faster code execution than the “if-else if” statement. It is easy to debug and maintain the programs using switch statements. The switch statement has faster execution power.

Is switch case faster than if-else?

As it turns out, the switch statement is faster in most cases when compared to if-else , but significantly faster only when the number of conditions is large. The primary difference in performance between the two is that the incremental cost of an additional condition is larger for if-else than it is for switch .

How is switch case different from if-else?

In the case of 'if-else' statement, either the 'if' block or the 'else' block will be executed based on the condition. In the case of the 'switch' statement, one case after another will be executed until the break keyword is not found, or the default statement is executed.


2 Answers

Summarising my initial post and comments - there are several advantages of switch statement over if/else statement:

  1. Cleaner code. Code with multiple chained if/else if ... looks messy and is difficult to maintain - switch gives cleaner structure.

  2. Performance. For dense case values compiler generates jump table, for sparse - binary search or series of if/else, so in worst case switch is as fast as if/else, but typically faster. Although some compilers can similarly optimise if/else.

  3. Test order doesn't matter. To speed up series of if/else tests one needs to put more likely cases first. With switch/case programmer doesn't need to think about this.

  4. Default can be anywhere. With if/else default case must be at the very end - after last else. In switch - default can be anywhere, wherever programmer finds it more appropriate.

  5. 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 with if/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 forgot break).

Thanks to all commenters.

like image 152
qrdl Avatar answered Sep 27 '22 19:09

qrdl


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"?

like image 22
Eric H. Avatar answered Sep 27 '22 20:09

Eric H.