Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Switch case Statement Implemented or works internally?

I read somewhere that the switch statement uses "Binary Search" or some sorting techniques to exactly choose the correct case and this increases its performance compared to else-if ladder.

And also if we give the case in order does the switch work faster? is it so? Can you add your valuable suggestions on this?

We discussed here about the same and planned to post as a question.

like image 763
2vision2 Avatar asked Dec 28 '12 09:12

2vision2


People also ask

How does a switch case work internally?

The order of the switch statements as such doesn't matter, it will do the same thing whether you have the order in ascending, descending or random order - do what makes most sense with regard to what you want to do. If nothing else, switch is usually a lot easier to read than an if-else sequence.

How does a switch statement works?

The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.

How does switch case works explain with an example?

Switch Case Example in CA switch construct is used to compare the value stored in variable num and execute the block of statements associated with the matched case. In this program, since the value stored in variable num is eight, a switch will execute the case whose case-label is 8.

Can we use or inside switch case?

The switch-case construct is pretty similar to an if-else statement, you can use the OR operator in an if however.


3 Answers

It's actually up to the compiler how a switch statement is realized in code.

However, my understanding is that when it's suitable (that is, relatively dense cases), a jump table is used.

That would mean that something like:

switch(i) {
  case 0: doZero(); break;
  case 1: doOne();
  case 2: doTwo(); break;
  default: doDefault();
}

Would end up getting compiled to something like (horrible pseudo-assembler, but it should be clear, I hope).

load i into REG
compare REG to 2
if greater, jmp to DEFAULT
compare REG to 0
if less jmp to DEFAULT
jmp to table[REG]
data table
  ZERO
  ONE
  TWO
end data
ZERO: call doZero
jmp END
ONE: call doOne
TWO: call doTwo
jmp END
DEFAULT: call doDefault
END:

If that's not the case, there are other possible implementations that allow for some extent of "better than a a sequence of conditionals".

like image 140
Vatine Avatar answered Sep 29 '22 04:09

Vatine


How swtich is implemented depends on what values you have. For values that are close in range, the compiler will generally generate a jump table. If the values are far apart, it will generate a linked branch, using something like a binary search to find the right value.

The order of the switch statements as such doesn't matter, it will do the same thing whether you have the order in ascending, descending or random order - do what makes most sense with regard to what you want to do.

If nothing else, switch is usually a lot easier to read than an if-else sequence.

like image 36
Mats Petersson Avatar answered Sep 29 '22 05:09

Mats Petersson


On some googling I found some interestin link and planned to post as an answer to my question. http://www.codeproject.com/Articles/100473/Something-You-May-Not-Know-About-the-Switch-Statem

Comments are welcome..

like image 41
2vision2 Avatar answered Sep 29 '22 05:09

2vision2