473,390 Members | 900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 12908
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,065 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,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

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,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.
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,065 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

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

Similar topics

4
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...
11
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.,...
2
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...
2
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
8
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...
7
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...
3
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...
13
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...
17
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
0
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,...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.