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 12906
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Angelos |
last post by:
Ok... I have to make this administation area where I have multiple Contents
to add edit delete publish .
The problem is that I don't know what is the best way of making the forms.
At the moment I...
|
by: Ohaya |
last post by:
Hi,
I'm trying to understand a situation where ASP seems to be "blocking" of
"queuing" requests. This is on a Win2K Advanced Server, with IIS5.
I've seen some posts (e.g.,...
|
by: Guadala Harry |
last post by:
After RTFM, I hope I have missed something: I would like to have a switch()
statement process multiple input values the same way - but without having
multiple case: blocks for multiple input values...
|
by: Mike P |
last post by:
Is it not possible to declare variables within a switch statement? My
variables don't seem to ever be in scope to be able to assign values to
them.
Thanks,
Mike
|
by: fonzie |
last post by:
Is it possible to have a data entry form where the information is
stored in several different tables (5 or 6)? I have an inventory
database where Table1 stores all of the data common to all...
|
by: MD |
last post by:
Hi,
I would like to access "variables" defined in my Python program in
a C module extension for Python. Is this possible? I looked at the
Python C API reference but didn't find anything there...
|
by: Major Doug |
last post by:
Hello:
situation--I have a research database. Each record in the database consists of 10 fields. I used access97 to rack/stack my database; very easy in the beginning. I created a form with a...
|
by: mcfly1204 |
last post by:
I have numerous strings that I would like to run though a switch statement. These strings are terms such as "None" or "Undefined", that I would like to set to Null. My first thought was to create...
|
by: Daniel |
last post by:
When I use the CreateThread API method, what do I need to do when I want to
pass more than one parameter where LPVOID lpParameter is passed?
Daniel
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |