By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

Multiple Control Variables in a Switch Statement (C)

100+
P: 208
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
Share this Question
Share on Google+
16 Replies


ilikepython
Expert 100+
P: 844
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

100+
P: 208
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
Expert Mod 5K+
P: 8,916
else if

not

elif


Unless of course they are preprocessor statements
Jun 1 '07 #4

ilikepython
Expert 100+
P: 844
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
Expert 100+
P: 1,764
Oh, Duuh!! It's like that in python, sorry.
Haha

I just hate when I mix laanguages.

Savage
Jun 1 '07 #6

100+
P: 208
I knew what you were saying ilikepython
Jun 1 '07 #7

AdrianH
Expert 100+
P: 1,251
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
Expert Mod 5K+
P: 8,916
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
Expert 100+
P: 1,251
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
Expert Mod 5K+
P: 8,916
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
Expert 100+
P: 1,251
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
Expert 100+
P: 1,764
Don't try this at home fokes, I'm a trained professional. ;) :D
Hahaha,I like this..

Savage
Jun 2 '07 #13

Banfa
Expert Mod 5K+
P: 8,916
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
Expert 100+
P: 1,764
Yes and I am Al Borland :D
U are?

:)

Savage
Jun 3 '07 #15

AdrianH
Expert 100+
P: 1,251
Yes and I am Al Borland :D
Is that guy related to Borland the SW company?


Adrian
Jun 3 '07 #16

AdrianH
Expert 100+
P: 1,251
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.