C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to simplify many OR in if statement?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Alan
Guest





PostPosted: Tue Dec 23, 2003 9:56 am    Post subject: how to simplify many OR in if statement? Reply with quote



I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....

thank you


Back to top
Robert Stankowic
Guest





PostPosted: Tue Dec 23, 2003 10:17 am    Post subject: Re: how to simplify many OR in if statement? Reply with quote




"Alan" <alanchoiyimlung (AT) sinatown (DOT) com> schrieb im Newsbeitrag
news:bs94kf$nte$1 (AT) news (DOT) hgc.com.hk...
Quote:
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....

ITYM
if(val == 5 || val == 9......

If there is no systematic in the values of val, the only thing I can think
of is a switch:

switch(val)
{
case 5:
case 9:
case 34:
....
case 234:
<do your stuff here>
break;
}

But I'd rethink the approach if it requires such a mass of or's

Robert



Back to top
Jeff Schwab
Guest





PostPosted: Tue Dec 23, 2003 10:21 am    Post subject: Re: how to simplify many OR in if statement? Reply with quote



Alan wrote:
Quote:
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....

thank you

Please don't cross-post.

I assume you want to be able to do something like this:

int main( )
{
int var = 3; // Initialize to whatever...

if( in_set( var ) )
{
// ...
}
}

The long "or" statement is not a bad way to go, unless you have a
really, really large set of numbers to check. Here is a couple of
alternatives:

/* C or C++ */

int in_set( int i )
{
int result = 0;

switch( i )
{
case 5:
case 9:
case 34:
case 111:
case 131:
result = 1;
}

return result;
}


/* C++ only */

namespace
{
int values[ ] = { 5, 9, 34, 111, 131 /* , ... */ };
int num_values = sizeof values / sizeof *values;

std::set< int > value_set( values, values + num_values );

inline bool in_set( int i )
{
return value_set.find( i ) != value_set.end( );
}
}


Back to top
Mike Hewson
Guest





PostPosted: Tue Dec 23, 2003 10:39 am    Post subject: Re: how to simplify many OR in if statement? Reply with quote

"Alan" <alanchoiyimlung (AT) sinatown (DOT) com> wrote

Quote:
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....

Don't know of any evident simplification, assuming the logic of the
problem actually *requires* some common code to be executed for those many
different values of 'val'.

One can *restate* using De Morgan's theorem:

if( !(val <> 5 && val <> 9 && val <> 34 && val <> 111 && val <> 131 &&
........) )
// Common code.

which flicks the OR's, but alas no more simply!

You could create a boolean array, and use val as an index for it, with
the array elements indicating a 'yes' for 5,9,34,111,131...... ( and 'no'
otherwise ).

if( boolean_array[val] == true )
// Common code.

but then one also needs memory for the array, and some initialization of
it's contents to boot.

What is the context of your logical expression?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal



Back to top
Christopher Benson-Manica
Guest





PostPosted: Tue Dec 23, 2003 2:21 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

In comp.lang.c Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote:

Quote:
Please don't cross-post.

You mean "Please cross-post carefully." comp.lang.c++ was clearly not
warranted, but there's nothing wrong with this going to acllcc++...


Quote:
switch( i )
{
case 5:
case 9:
case 34:
case 111:
case 131:
result = 1;
}

I rather prefer this method myself, FWIW.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Back to top
Ron Natalie
Guest





PostPosted: Tue Dec 23, 2003 2:26 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote


"Mike Hewson" <hewsmike (AT) austarnet (DOT) com.au> wrote


Quote:

if( !(val <> 5 && val <> 9 && val <> 34 && val <> 111 && val <> 131 &&

And what language is this? Looks like some mutant cross between C++ and Fortran.


Back to top
Cy Edmunds
Guest





PostPosted: Tue Dec 23, 2003 2:51 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:

"Mike Hewson" <hewsmike (AT) austarnet (DOT) com.au> wrote



if( !(val <> 5 && val <> 9 && val <> 34 && val <> 111 && val <> 131
&&

And what language is this? Looks like some mutant cross between C++ and
Fortran.


It's that De Morgan guy. Couldn't program his way out of a paper bag.

--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
Jeff Schwab
Guest





PostPosted: Tue Dec 23, 2003 4:22 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

Christopher Benson-Manica wrote:
Quote:
In comp.lang.c Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote:


Please don't cross-post.


You mean "Please cross-post carefully." comp.lang.c++ was clearly not
warranted, but there's nothing wrong with this going to acllcc++...

Right you are!

Quote:
switch( i )
{
case 5:
case 9:
case 34:
case 111:
case 131:
result = 1;
}


I rather prefer this method myself, FWIW.

I would like this if I knew a suitable jump-table would be generated in
the code, but for such disparate values... Anyway, I actually *did*
like Mike's suggestion of creating an array of boolean values:

if( boolean_array[val] == true )
// Common code.


-Jefff


Back to top
Keith Thompson
Guest





PostPosted: Tue Dec 23, 2003 6:57 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

Jeff Schwab <jeffplus (AT) comcast (DOT) net> writes:
[...]
Quote:
I would like this if I knew a suitable jump-table would be generated
in the code, but for such disparate values... Anyway, I actually
*did* like Mike's suggestion of creating an array of boolean values:

if( boolean_array[val] == true )
// Common code.

Even assuming you have an appropriate definition for "true", the
comparison is unnecesary. In general, explicitly comparing boolean
values to "true" or "false" is unnecessary and potentially dangerous.
It's already a boolean, after all.

C (unless you have C99) has no predefined boolean type. In both C and
C++, any non-zero integer value is considered true in a condition; if
your "true" is defined as 1, the comparison will fail for any value
other than 0 or 1.

Just use

if ( boolean_array[val] ) {
/* blah blah */
}

(If you think that "boolean_array[val] == true" is clearer, you should
think that "(boolean_array[val] == true) == true" is even clearer.)

Note that this argument doesn't apply to non-boolean values used as
conditions. A pointer value, for example, can be used as a condition
(implicitly testing whether it's a null pointer), but many programmers
prefer to make the comparison explicit; "if (p)" and "if (p != NULL)"
are equally valid.

--
Keith Thompson (The_Other_Keith) [email]kst-u (AT) mib (DOT) org[/email] <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)

Back to top
Jeff Schwab
Guest





PostPosted: Tue Dec 23, 2003 7:03 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

Keith Thompson wrote:
Quote:
Jeff Schwab <jeffplus (AT) comcast (DOT) net> writes:
[...]

I would like this if I knew a suitable jump-table would be generated
in the code, but for such disparate values... Anyway, I actually
*did* like Mike's suggestion of creating an array of boolean values:

if( boolean_array[val] == true )
// Common code.


Even assuming you have an appropriate definition for "true", the
comparison is unnecesary. In general, explicitly comparing boolean
values to "true" or "false" is unnecessary and potentially dangerous.
It's already a boolean, after all.

I agree completely; I was copying someone Mike's code. Anyway, I see
nothing inherently wrong with "== true," even though I prefer to skip it.

Quote:
Note that this argument doesn't apply to non-boolean values used as
conditions. A pointer value, for example, can be used as a condition
(implicitly testing whether it's a null pointer), but many programmers
prefer to make the comparison explicit; "if (p)" and "if (p != NULL)"
are equally valid.

That's exactly the same situation. I also prefer "if( p )" to "if p !=
NULL )," but I see no need to be religious about it. The one that
really bugs me is seeing someone "correct" this:

if( p == NULL )

to this:

if( NULL == p )

This is supposed to help avoid accidental assignment in the test.
However, I think "if( p )" avoids the same problem in a much shorter,
more readable way.

Anway, different key-strokes for different folks, I guess.

-Jeff


Back to top
John Bode
Guest





PostPosted: Tue Dec 23, 2003 7:05 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

"Alan" <alanchoiyimlung (AT) sinatown (DOT) com> wrote

Quote:
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....

thank you

First of all, I think you want the logical OR operator || instead of
the bitwise OR operator |. Other than that...

Personally, I would put this test into its own function.

#ifndef TRUE
#define TRUE (1)
#define FALSE (0)
#endif
...
int valueInSet (int val, int *set, int setsize)
{
int i;

for (i = 0; i < setsize; i++)
{
if (val == set[i])
return TRUE;
}

return FALSE;
}

int main (void)
{
int testSet[] = {5, 9, 34, 111, 131, ...};
int setSize = (int) (sizeof testSet / sizeof testSet[0]);
int val;
...
if (valueInSet (val, testSet, setSize))
{
/* do something */
}
...
return 0;
}

Yes, it introduces a bit more overhead than just writing out the test
longhand (either using the sequential OR approach or by using the
switch structure others have suggested). The tradeoff is that this
solution is more general. If your test set changes, you don't have to
actually muck with the program logic, just the contents of the test
set.

With some time I could probably come up with something a bit more
clever, but I think this is a good solution.

Back to top
Robert Bachmann
Guest





PostPosted: Tue Dec 23, 2003 7:16 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

Ron Natalie wrote:
Quote:
"Mike Hewson" <hewsmike (AT) austarnet (DOT) com.au> wrote


if( !(val <> 5 && val <> 9 && val <> 34 && val <> 111 && val <> 131 &&

And what language is this? Looks like some mutant cross between C++ and Fortran.

C mixed up with Pascal, Basic or some other language which uses <> for

the inequality operator.

It should be rather:
if( !(val != 5 && val != 9 && val != 34 && val != 111 &&/*etc*/

--
[email]Robert.Bachmann (AT) rbdev (DOT) net[/email]

Back to top
Christian Bau
Guest





PostPosted: Tue Dec 23, 2003 7:51 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

In article <bs94kf$nte$1 (AT) news (DOT) hgc.com.hk>,
"Alan" <alanchoiyimlung (AT) sinatown (DOT) com> wrote:

Quote:
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)

Have a look at the || operator.

Back to top
Mike Hewson
Guest





PostPosted: Tue Dec 23, 2003 9:02 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:

"Mike Hewson" <hewsmike (AT) austarnet (DOT) com.au> wrote



if( !(val <> 5 && val <> 9 && val <> 34 && val <> 111 && val <> 131
&&

And what language is this? Looks like some mutant cross between C++ and
Fortran.


Whoops, my brain fart, sorry!
I was doing VBScripting at the time, and slipped a cog reloading the old
task state segment.
( grumble ..Damned multitasking....grumble ).

Happy to have amused you all, though :-)

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal



Back to top
Mike Hewson
Guest





PostPosted: Tue Dec 23, 2003 9:03 pm    Post subject: Re: how to simplify many OR in if statement? Reply with quote

"Keith Thompson" <kst-u (AT) mib (DOT) org> wrote

Quote:
Jeff Schwab <jeffplus (AT) comcast (DOT) net> writes:
[...]
I would like this if I knew a suitable jump-table would be generated
in the code, but for such disparate values... Anyway, I actually
*did* like Mike's suggestion of creating an array of boolean values:

if( boolean_array[val] == true )
// Common code.

Even assuming you have an appropriate definition for "true", the
comparison is unnecesary.

Sorry ( again ), I meant to write 'yes' - meaning whatever one had coded in
the array as being that.

Quote:
In general, explicitly comparing boolean values to "true" or "false" is
unnecessary and potentially > dangerous.


Indeed, please tell.....

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.