Is it possible to have multiple control variables in a switch statement?
For example: - switch(a , b)
-
{
-
case(a>b):
-
case(b<a):
-
case(a==b):
-
}
(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?
16 12722
Is it possible to have multiple control variables in a switch statement?
For example: - switch(a , b)
-
{
-
case(a>b):
-
case(b<a):
-
case(a==b):
-
}
(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: -
if (a > b){}
-
elif (a < b){}
-
elif (a == b){}
-
Nope, I don't think so. You can easily do the same thing with if statements though: -
if (a > b){}
-
elif (a < b){}
-
elif (a == b){}
-
True...Hmm ok thanks
Banfa 9,065
Expert Mod 8TB
else if
not
elif
Unless of course they are preprocessor statements
else if
not
elif
Unless of course they are preprocessor statements
Oh, Duuh!! It's like that in python, sorry.
Oh, Duuh!! It's like that in python, sorry.
Haha
I just hate when I mix laanguages.
Savage
I knew what you were saying ilikepython
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
Banfa 9,065
Expert Mod 8TB
There are various tricks you can pull with switch statements, here's 1 I've seen in a number of projects -
typedef struct {
-
int member1;
-
int member2;
-
} MyStruct;
-
-
void MyFunction(void)
-
{
-
switch(true)
-
{
-
case (sizeof MyStruct == 8):
-
case 0:
-
break;
-
}
-
}
There are various tricks you can pull with switch statements, here's 1 I've seen in a number of projects -
typedef struct {
-
int member1;
-
int member2;
-
} MyStruct;
-
-
void MyFunction(void)
-
{
-
switch(true)
-
{
-
case (sizeof MyStruct == 8):
-
case 0:
-
break;
-
}
-
}
Cute, but what exactly does that get you except code obfustication?
Adrian
Banfa 9,065
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.
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 -
typedef struct {
-
int member1;
-
int member2;
-
} MyStruct;
-
-
void MyFunction(void)
-
{
-
int test[(sizeof MyStruct == 8) ? 1 : -1];
-
}
-
Well actually that is something like the expanded version, it was done using macros: -
#define CAT(x,y) x ## y
-
#define PREASSERT(x) \
-
static void CAT(PREASSERT_,__LINE__)(void) \
-
{ \
-
int test[(x) ? 1 : -1]; \
-
}
-
-
PREASSERT(sizeof MyStruct == 8)
-
or something like that.
Don't try this at home fokes, I'm a trained professional. ;) :D
Adrian
Don't try this at home fokes, I'm a trained professional. ;) :D
Hahaha,I like this..
Savage
Banfa 9,065
Expert Mod 8TB
Don't try this at home fokes, I'm a trained professional. ;) :D
Yes and I am Al Borland :D
Yes and I am Al Borland :D
U are?
:)
Savage
Yes and I am Al Borland :D
Is that guy related to Borland the SW company?
Adrian
Ah, I see. I've seen something else like that.. mmmm -
typedef struct {
-
int member1;
-
int member2;
-
} MyStruct;
-
-
void MyFunction(void)
-
{
-
int test[(sizeof MyStruct == 8) ? 1 : -1];
-
}
-
Well actually that is something like the expanded version, it was done using macros: -
#define CAT(x,y) x ## y
-
#define PREASSERT(x) \
-
static void CAT(PREASSERT_,__LINE__)(void) \
-
{ \
-
int test[(x) ? 1 : -1]; \
-
}
-
-
PREASSERT(sizeof MyStruct == 8)
-
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: - #define CAT(x,y) x ## y
-
#define PREASSERT(x) \
-
void CAT(PREASSERT_,__LINE__)(int assertTest[(x) ? 1 : -1])
-
-
PREASSERT(sizeof MyStruct == 8);
-
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
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
|
11 posts
views
Thread by Ohaya |
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
|
7 posts
views
Thread by MD |
last post: by
| | |
17 posts
views
Thread by Daniel |
last post: by
| | | | | | | | | | |