top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Why can switch only compare to const values?

+1 vote
31 views

Just curious:

Take a switch-statement like -

switch(myVar)
{
    case(label):
    ...
    break;
}

why label should be constant only?

posted Mar 22, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

3 Answers

+2 votes
 
Best answer

Because it can be ambiguous otherwise:

int a = 1, b = 1;
int val = 1;

switch (val) {
case a: foo(); break;
case b: bar(); break;
}

Requiring constant expressions for the cases makes sure that there's
no ambiguity, since they can be evaluated at compile time.

answer Mar 22, 2013 by anonymous
0 votes

Because thats how its defined in The Standard.
Use if() if you want to compare variables.

answer Mar 22, 2013 by anonymous
0 votes

How would you be able to define behavior by comparing to a variable
that ranges over a large set of values? How do you limit what values
the variable can have? It wouldnt even make sense to compare to a
variable, you are defining the behavior in the separate statements in
a switch based on a specific state, it amounts to using an if
statement... if myVar=1, do this, if myVar=2, do this, and so on. If
you need to use a range of values, you would need to use an explicit
if: if myVar > 0 && myVar < 100, do this, if myVar > 100 & If I were
to divulge it, it would overturn the world."

answer Mar 22, 2013 by anonymous
Compiler will be optimised for switch statement. If there are case
from 1 to 100 and the value of switching variable is 76, then the code will
not run like compare case 1, case 2, case 3,... Case 76. The code will be
optimised to reach case 76 much more faster by using some search
methods.
This optimisation can only happen if we have a sequential variable
which is obviously integers.

Theres no searching on most common platforms. An optimizing compiler
 will build a "jump table" (a list of addresses) and simply branch to
the  target code based on the index specified in the jump table.

 But I posit that if you have 76 case statements in a single switch
 block, youre probably doing something wrong and need to refactor your

 code. I realize you meant the 76 as an example though because youve
 never written any code like that.
Similar Questions
+4 votes

Sorry for my philosophical Question, any thoughts :)
Hope it would not be deleted.

0 votes

I have a function which takes string as an argument, but while calling this function I am actually passing char * (not const char *). This function uses the argument and populate the private variable which is of string type.

In the product its showing some strange behaviour i.e. crashing once in a while (not always) in other function while accessing the string variable which is class variable. Just want to check if the issue is with the passing of the variable which is of char * type.

void myfunc(char * Id)
{
    abc.fetchInfo(Id);
    ...
    ...
}

Definition of the fetchInfo 
fetchInfo(std::string Id);

Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...