Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the switch statement and not if-else?

I've been wondering this for some time now. I'm by far not a hardcore programmer, mainly small Python scripts and I've written a couple molecular dynamics simulations. For the real question: What is the point of the switch statement? Why can't you just use the if-else statement?

Thanks for your answer and if this has been asked before please point me to the link.

EDIT

S.Lott has pointed out that this may be a duplicate of questions If/Else vs. Switch. If you want to close then do so. I'll leave it open for further discussion.

like image 925
Nope Avatar asked Jan 16 '09 02:01

Nope


People also ask

What is the difference between if-else and switch statement?

An if-else statement can evaluate almost all the types of data such as integer, floating-point, character, pointer, or Boolean. A switch statement can evaluate either an integer or a character. In the case of 'if-else' statement, either the 'if' block or the 'else' block will be executed based on the condition.

Which is faster switch or if-else?

Most would consider the switch statement in this code to be more readable than the if-else statement. 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.

Why switch statement is better than multiple if statements?

Switch statement works better than multiple if statements when you are giving input directly without any condition checking in the statements. Switch statement works well when you want to increase the readability of the code and many alternative available.


2 Answers

A switch construct is more easily translated into a jump (or branch) table. This can make switch statements much more efficient than if-else when the case labels are close together. The idea is to place a bunch of jump instructions sequentially in memory and then add the value to the program counter. This replaces a sequence of comparison instructions with an add operation.

Below are some extremely simplified psuedo-assembly examples. First, the if-else version:

    // C version     if (1 == value)         function1();     else if (2 == value)         function2();     else if (3 == value)         function3();      // assembly version     compare value, 1     jump if zero label1     compare value, 2     jump if zero label2     compare value, 3     jump if zero label3 label1:     call function1 label2:     call function2 label3:     call function3 

Next is the switch version:

    // C version     switch (value) {     case 1: function1(); break;     case 2: function2(); break;     case 3: function3(); break;     }      // assembly version     add program_counter, value     call function1     call function2     call function3 

You can see that the resulting assembly code is much more compact. Note that the value would need to be transformed in some way to handle other values than 1, 2 and 3. However, this should illustrate the concept.

like image 63
Judge Maygarden Avatar answered Sep 22 '22 03:09

Judge Maygarden


Switch can be optimized by compiler - you will get faster code.
Also I find it to be more elegant when dealing with enumerable types.

To sum up switch statement gives you performance + code elegance :)

Here are some useful links:

like image 38
aku Avatar answered Sep 21 '22 03:09

aku