469,362 Members | 2,470 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Multiple Control Variables in a Switch Statement (C)

208 100+
Is it possible to have multiple control variables in a switch statement?

For example:

Expand|Select|Wrap|Line Numbers
  1. switch(a , b)
  2. {
  3.        case(a>b):
  4.        case(b<a):
  5.        case(a==b):
  6. }
(I know you can't separate it by a comma...I've tried....I also know that there aren't any breaks or anythign like that. I'm not asking about syntax of a switch statement, just if you can have multiple control variables) but is there any other way to do this?
Jun 1 '07 #1
16 12536
ilikepython
844 Expert 512MB
Is it possible to have multiple control variables in a switch statement?

For example:

Expand|Select|Wrap|Line Numbers
  1. switch(a , b)
  2. {
  3.        case(a>b):
  4.        case(b<a):
  5.        case(a==b):
  6. }
(I know you can't separate it by a comma (I've tried) but is there any other way to do this?
Nope, I don't think so. You can easily do the same thing with if statements though:
Expand|Select|Wrap|Line Numbers
  1. if (a > b){}
  2. elif (a < b){}
  3. elif (a == b){}
  4.  
Jun 1 '07 #2
Silent1Mezzo
208 100+
Nope, I don't think so. You can easily do the same thing with if statements though:
Expand|Select|Wrap|Line Numbers
  1. if (a > b){}
  2. elif (a < b){}
  3. elif (a == b){}
  4.  
True...Hmm ok thanks
Jun 1 '07 #3
Banfa
9,064 Expert Mod 8TB
else if

not

elif


Unless of course they are preprocessor statements
Jun 1 '07 #4
ilikepython
844 Expert 512MB
else if

not

elif


Unless of course they are preprocessor statements
Oh, Duuh!! It's like that in python, sorry.
Jun 1 '07 #5
Savage
1,764 Expert 1GB
Oh, Duuh!! It's like that in python, sorry.
Haha

I just hate when I mix laanguages.

Savage
Jun 1 '07 #6
Silent1Mezzo
208 100+
I knew what you were saying ilikepython
Jun 1 '07 #7
AdrianH
1,251 Expert 1GB
switch only takes one 'parameter' which is an integral number (used to be transformed to an int). When using an enum, some compilers will complain if you havn't covered all of the enums.

This is all done this way so that the compiler can optimise the jump to the correct code useing a jump table if it is densely packed or cluster packed, or can degrade into something resembling an if (num == value1) {} else if (num == value2) ... if the values are spread out significantly.


Adrian
Jun 1 '07 #8
Banfa
9,064 Expert Mod 8TB
There are various tricks you can pull with switch statements, here's 1 I've seen in a number of projects

Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     int member1;
  3.     int member2;
  4. } MyStruct;
  5.  
  6. void MyFunction(void)
  7. {
  8.     switch(true)
  9.     {
  10.     case (sizeof MyStruct == 8):
  11.     case 0:
  12.         break;
  13.     }
  14. }
Jun 1 '07 #9
AdrianH
1,251 Expert 1GB
There are various tricks you can pull with switch statements, here's 1 I've seen in a number of projects

Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     int member1;
  3.     int member2;
  4. } MyStruct;
  5.  
  6. void MyFunction(void)
  7. {
  8.     switch(true)
  9.     {
  10.     case (sizeof MyStruct == 8):
  11.     case 0:
  12.         break;
  13.     }
  14. }
Cute, but what exactly does that get you except code obfustication?


Adrian
Jun 2 '07 #10
Banfa
9,064 Expert Mod 8TB
Cute, but what exactly does that get you except code obfustication?
It's a compile time check for structure size, it only compiles if sizeof MyStruct == 8.

In 1 or 2 obscurish instances there has been a requirement that a structure wasn't changed and that is the closest thing you can get to automated checking.

Obviously when in use you put a large comment in explaining what to do or who to see in case of an error generated there.
Jun 2 '07 #11
AdrianH
1,251 Expert 1GB
It's a compile time check for structure size, it only compiles if sizeof MyStruct == 8.

In 1 or 2 obscurish instances there has been a requirement that a structure wasn't changed and that is the closest thing you can get to automated checking.

Obviously when in use you put a large comment in explaining what to do or who to see in case of an error generated there.
Ah, I see. I've seen something else like that.. mmmm
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     int member1;
  3.     int member2;
  4. } MyStruct;
  5.  
  6. void MyFunction(void)
  7. {
  8.    int test[(sizeof MyStruct == 8) ? 1 : -1];
  9. }
  10.  
Well actually that is something like the expanded version, it was done using macros:
Expand|Select|Wrap|Line Numbers
  1. #define CAT(x,y) x ## y
  2. #define PREASSERT(x) \
  3. static void CAT(PREASSERT_,__LINE__)(void) \
  4. { \
  5.   int test[(x) ? 1 : -1]; \
  6. }
  7.  
  8. PREASSERT(sizeof MyStruct == 8)
  9.  
or something like that.

Don't try this at home fokes, I'm a trained professional. ;) :D


Adrian
Jun 2 '07 #12
Savage
1,764 Expert 1GB
Don't try this at home fokes, I'm a trained professional. ;) :D
Hahaha,I like this..

Savage
Jun 2 '07 #13
Banfa
9,064 Expert Mod 8TB
Don't try this at home fokes, I'm a trained professional. ;) :D
Yes and I am Al Borland :D
Jun 3 '07 #14
Savage
1,764 Expert 1GB
Yes and I am Al Borland :D
U are?

:)

Savage
Jun 3 '07 #15
AdrianH
1,251 Expert 1GB
Yes and I am Al Borland :D
Is that guy related to Borland the SW company?


Adrian
Jun 3 '07 #16
AdrianH
1,251 Expert 1GB
Ah, I see. I've seen something else like that.. mmmm
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     int member1;
  3.     int member2;
  4. } MyStruct;
  5.  
  6. void MyFunction(void)
  7. {
  8.    int test[(sizeof MyStruct == 8) ? 1 : -1];
  9. }
  10.  
Well actually that is something like the expanded version, it was done using macros:
Expand|Select|Wrap|Line Numbers
  1. #define CAT(x,y) x ## y
  2. #define PREASSERT(x) \
  3. static void CAT(PREASSERT_,__LINE__)(void) \
  4. { \
  5.   int test[(x) ? 1 : -1]; \
  6. }
  7.  
  8. PREASSERT(sizeof MyStruct == 8)
  9.  
or something like that.

Don't try this at home fokes, I'm a trained professional. ;) :D


Adrian
Actually, now that I think about it, I think this will work better:
Expand|Select|Wrap|Line Numbers
  1. #define CAT(x,y) x ## y
  2. #define PREASSERT(x) \
  3. void CAT(PREASSERT_,__LINE__)(int assertTest[(x) ? 1 : -1])
  4.  
  5. PREASSERT(sizeof MyStruct == 8);
  6.  
Which works in more places than the previous one I defined. Basicly it is declaring a function which can be done inside of functions, classes, in the global space. You can't call it since it has no body so it only adds to the symbol table and not to text or data segments.


Adrian
Jun 3 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Angelos | last post: by
2 posts views Thread by Guadala Harry | last post: by
2 posts views Thread by Mike P | last post: by
8 posts views Thread by fonzie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.