473,667 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Condensing nested switch statements

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
19 15181
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


Yes: why? This code isn't really obfuscated. Ok, the _design_ probably
isn't all that hot, with the two different control variables, but
there's little you can do about that without changing the semantics.

Richard
Nov 14 '05 #2
nrk
Christopher Benson-Manica wrote:
On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


You can condense it, but I don't consider the following to be an improvement
(hopefully, I haven't changed the semantics either):

int ret;

if ( int_val_1 != !!int_val_1 ) return true;
switch ( int_val_2 ) {
case enum_val_1: ret = (num1 == num2); break;
case enum_val_2: ret = (num1 <= num2); break;
case enum_val_3: ret = (num1 >= num2); break;
default: return true;
}
return ret ^ int_val_1;

Less readable, less flexible, more error-prone. Wow!! If job security was a
priority, I wouldn't hesitate to incorporate this "improvemen t" :-)

-nrk.

--
Remove devnull for email
Nov 14 '05 #3
Christopher Benson-Manica wrote:

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


int result;

switch (int_val_2) {
case enum_val_1: result = num1 == num2;
case enum_val_2: result = num1 <= num2;
case enum_val_3: result = num1 >= num2;
default: return true; /* sounds ridiculous */
}
if (1 == int_val1) result = !result;
else if (0 != int_val1) result = true;
/* else use_result for 0 */
return result;

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #4
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote in
news:bv******** **@chessie.cirr .com:
On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


Get rid of the default cases?

switch (int_val_1)
{

case 0:
switch (int_val_2)
{
case enum_val_1: return num1 == num2;
case enum_val_2: return num1 <= num2;
case enum_val_3: return num1 >= num2;
}
break;

case 1:
switch (int_val_2)
{
case enum_val_1: return num1 != num2;
case enum_val_2: return num1 > num2;
case enum_val_3: return num1 < num2;
}
break;
}

return true; /* factor this out */

It's not much, admittedly.

--
- Mark ->
--
Nov 14 '05 #5
nrk
CBFalconer wrote:
Christopher Benson-Manica wrote:

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


int result;

switch (int_val_2) {
case enum_val_1: result = num1 == num2;
case enum_val_2: result = num1 <= num2;
case enum_val_3: result = num1 >= num2;
default: return true; /* sounds ridiculous */
}
if (1 == int_val1) result = !result;
else if (0 != int_val1) result = true;
/* else use_result for 0 */
return result;


Your incantations have invoked the demons that reside in fall-thru hell :-)
I believe this is also known as the devil's device.

-nrk.
--
Remove devnull for email
Nov 14 '05 #6
On Tue, 3 Feb 2004 15:42:07 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


Not counting the default cases you have 6 = 2*3 distinct results. So
you will definitely need 6 distinct cases. What's wrong with the code?

--
Horst

Nov 14 '05 #7
CBFalconer wrote:

Christopher Benson-Manica wrote:

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


int result;

switch (int_val_2) {
case enum_val_1: result = num1 == num2;
case enum_val_2: result = num1 <= num2;
case enum_val_3: result = num1 >= num2;
default: return true; /* sounds ridiculous */
}
if (1 == int_val1) result = !result;
else if (0 != int_val1) result = true;
/* else use_result for 0 */
return result;


Woops! - Each case is missing a "break;" statement.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #8
begin followup to Christopher Benson-Manica:
switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}


Well, if the values of int_val_2 are contiguous then there is one
obvious solution. Replace "- 0" with the actual minimum value.

switch(int_val_ 1 * 3 + int_val_2 - 0)
{
case 0: return( num1 == num2 );
case 1: return( num1 <= num2 );
case 2: return( num1 >= num2 );
case 3: return( num1 != num2 );
case 4: return( num1 > num2 );
case 5: return( num1 < num2 );
default: return true;
}

--
Für Google, Tux und GPL!
Nov 14 '05 #9
Horst Kraemer <ho***********@ epost.de> spoke thus:
switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}
Not counting the default cases you have 6 = 2*3 distinct results. So
you will definitely need 6 distinct cases. What's wrong with the code?


6 distinct results, yes, but notice that they are related. After
reading replies to my other question, I gather that something like

switch( int_val_1 ) {
case 0:
case 1:
switch( int_val_2 ) {
case enum_val_1: return( (num1 == num2) ^ int_val_1 );
case enum_val_2: return( (num1 <= num2) ^ int_val_1 );
case enum_val_3: return( (num1 >= num2) ^ int_val_1 );
}
}
return 1;

would work, at the cost of some clarity.

(Sorry for the true's - I forgot to un-C++ my code...)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1746
by: Jeff Maestas | last post by:
I want to publish a patch CD. The CD will contain my files and an HTML page which will autorun, and tell the user which patch to install. (The Microsoft MSI installer v2 is comes in two different versions for 98/me and nt4/2000, and need to install that before my patches get installed. I need to give the users the correct MSI, so I need to know what their OS is. Most of our users are not computer people, and have trouble clicking the mouse...
3
6450
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
10
3220
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
13
11240
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch statement? The break keyword will only come out of the switch, not the for-next loop.
3
1528
by: G Patel | last post by:
Compiler won't let me nest switch statements with common labels inside them (in the cases). Why is this a problem (either in terms of standard or in terms of compiler's ability to make sense of it)? ex: typedef enum {ONE, TWO, THREE} TYPE; /* dumb use, but explains situation */
9
9324
by: paulyche | last post by:
I'm writing a program which contains an awful lot of nested if statements. I don't know how efficient this is but it definetely makes the code less readable. Does anyone have any advice on how I can improve the following kind of code? Apparently other languages have a switch statement which is useful here...Python seems to lack this functionality. My Python is self-taught so I'd appreciate any outside input. Lots of the code involves...
18
3634
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an abstract class that has the virtual function getId(). I then have a function that prints a special string when a bob meets another bob (all combinations of bobs has to meet and since the are schizophrenic they can also meet themselves):
18
1558
by: MZaza | last post by:
Hello, When I compile it I get an error in line 16, which got case 'c'. #include <cstdlib> #include <iostream> #include <math.h> using namespace std; int main()
13
2110
by: JRough | last post by:
I got lost on formatting the nested if/else's on the bottom of the file right up the last else/if there seems to be a stray bracket. I'm trying to line up the brackets so I can read it. Is it okay to split a long line setting a variable on two lines at the equals sign? That is so you can read it in google groups. <? //Send payement advices $TPL_auction_id = $auction_id; $user_id=$userrec;
0
8365
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8563
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8646
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7390
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.