Hi,
if any one has any idea "how the switch statement works".

does it maintain any tables internally or any other machanism?
and is there any way to make the switch statement efficient?

Recommended Answers

All 6 Replies

The switch statement just compares each case to whatever you are switching on, be it an int or a char. So I think there would be no tables, the assembly instructions would basically just subtract the case (ex 'q') from what you're comparing to it, (ex 'a') and then set a flag which it'd use to see if q was >, <, or = a.

The switch statement just compares each case to whatever you are switching on, be it an int or a char. So I think there would be no tables, the assembly instructions would basically just subtract the case (ex 'q') from what you're comparing to it, (ex 'a') and then set a flag which it'd use to see if q was >, <, or = a.

then it would be like if-else if internally.
then what is the advantage of using switch.

but here it is given as it uses jump tables

http://www.eecg.toronto.edu/~moshovos/ECE243-2009/lec8%20-%20Switch.htm

in some cases it needs to compare all the case constants to find the required one and how is it able to find the case though we follow some random order in giving the case constants.

Hi,
if any one has any idea "how the switch statement works".

does it maintain any tables internally or any other machanism?
and is there any way to make the switch statement efficient?

if you input two number then you get the product without using the arterisk

please give me a code to get the product without using the arterisk

then it would be like if-else if internally.

but here it is given as it uses jump tables

I think compilers commonly use jump tables for a tight range of cases and compares for wider ranges. This switch would probably use a jump table:

switch (condition)
{
    case 0:
        //...
        break;
    case 1:
        //...
        break;
    case 2:
        //...
        break;
    case 3:
        //...
        break;
    case 4:
        //...
        break;
    case 5:
        //...
        break;
}

But this switch would probably use compares and be close to the same result as the equivalent if statement because a jump table would be too big and too sparse:

switch (condition)
{
    case 0:
        //...
        break;
    case 10000:
        //...
        break;
    case 5000:
        //...
        break;
    case 40:
        //...
        break;
    case 900:
        //...
        break;
    case 123456:
        //...
        break;
}

I have heard about compilers using a jump table and binary search for switches that have a large number of cases and the range is wide.

then what is the advantage of using switch

If a switch is more intuitive then it makes the code clearer. Compilers are good at optimization, so you should not worry about which statement is more efficient and write code that is easy to read. The biggest performance changes come from whole algorithms anyway.

hey,
the switch statement will use if-else statements internally if the number of cases to be compared is very less (say upto 10) ... if thenumber of cases is very large , it uses a jump table for implementation , for e.g. if '33' is a CASE , the 33the position in the jump table will contain the machine code to go to the code which is to be executed in case of 33 . ... but if the range of numbers used is very large ... it has to use a hash table for efficiency ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.