if (one of several) alternatives

  • Thread starter Thread starter RC
  • Start date Start date
R

RC

I have some code that needs to know if an int variable is one of several
possible values.

What is a better way of performing the test than stacking a bunch of case:
in a switch statement? I also don't want to have a bunch of logical ORs in
an if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==
87....)

Any suggestions?

Thanks!
 
Hello!

What about:

switch (someInt)
{
case 25:
case 18:
case 87:
...
case 129:
{
... do work here
break;
}
}

Makes for a much higher readability, and I would think the performance is
better but perhaps someone could cast some light on this? Actually, I think
I'll try to compare the IL code to an "if" statement ..
 
RC said:
I have some code that needs to know if an int variable is one of several
possible values.

What is a better way of performing the test than stacking a bunch of case:
in a switch statement? I also don't want to have a bunch of logical ORs in
an if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==
87....)

Any suggestions?

I like the stacked case in a switch. There are lots of creative ways
you could do this. Here's one, just for fun:

int [] vals = {25,18,87};
int someInt = 25;
bool found = false;
if (((System.Collections.IList)vals).Contains(someInt))
found = true;
 
What is a better way of performing the test than stacking a bunch of case:
in a switch statement? I also don't want to have a bunch of logical ORs in
an if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==
87....)

Why not? Short of an if ... then ... else if ... else if... sequence, these
are the techniques that the C# language syntax offers you.

In the programming language of your dreams, what would the syntax look like?

Just curious,
Tom Dacon
Dacon Software Consulting
 
If this is a non trivial action, then on occassion you can convert the
code to a
polymorphic solution with an abstract base class with a virtual DoIt
method
and return a concrete derived class with a concrete DoIt method. This
eliminates the switch allowing you to invoke the base class DoIt method.
The
main reason to do this is that you should never need to update the
"switch"
logic for a new value, just load a new plug in at runtime or add a new
concrete class to the project.

Regards,
Jeff
 
<<In the programming language of your dreams, what would the syntax look
like?>>

It would look something like this...

if
(System.Polite.Comparisons.My.Dearest.Computer.Kindly.Tell.Me.At.Your.Earlie
st.Convenience.Whether.The.Input.Integer.Value.Matches.One.Of.These.Extraord
inary.Values({25, 18, 87}).Thank.You(inputInt)) {
//do one thing
}
else {
//do another
}
 
I've written a function to do this that looks like this:

public bool isIn(IComparable findThis, params IComparable [] itemsToLookIn)
{
foreach(object obj in itemsToLookIn)

{
if(findThis.CompareTo(obj) == 0) return true;
}

return false;
}

a more efficient version for ints would be:

public bool isIn(int findThis, params int [] itemsToLookIn)
{
foreach(object number in itemsToLookIn)

{
if(number == findThis) return true;
}

return false;
}

cheers,
mortb
 
I've also written a variant
public bool isIn2(int findThis, int [] itemsToLookIn)
{
foreach(object numberin itemsToLookIn)
{
if(number = findThis) return true;
}
return false;
}

public void foo()
{
// calling isIn2
if(isIn2(2, new int[] {2,3,4}))
{
// do your stuff
}
// calling isIn perhaps not as clear as isIn2 when reading the program
if(isIn(2,3,4,2,567 /*, etc... */)
{
// do your stuff
}
}

mortb said:
I've written a function to do this that looks like this:

public bool isIn(IComparable findThis, params IComparable []
itemsToLookIn)
{
foreach(object obj in itemsToLookIn)

{
if(findThis.CompareTo(obj) == 0) return true;
}

return false;
}

a more efficient version for ints would be:

public bool isIn(int findThis, params int [] itemsToLookIn)
{
foreach(object number in itemsToLookIn)

{
if(number == findThis) return true;
}

return false;
}

cheers,
mortb


RC said:
I have some code that needs to know if an int variable is one of several
possible values.

What is a better way of performing the test than stacking a bunch of
case:
in a switch statement? I also don't want to have a bunch of logical ORs
in
an if() statement (e.g., if (someInt == 25 || someInt == 18 || someInt ==
87....)

Any suggestions?

Thanks!
 
Hello!
If this is a non trivial action, then on occassion you can convert the
code to a polymorphic solution with an abstract base class with
a virtual DoIt

My impression was, that he was after the simple solution ;-)
 
The switch()...case is a better readable alternative. If you want a
different way of finding a value it could be done through a collection
or an array. Something like

int[] arr={12,187,24};
foreach (int i in arr)

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
RC said:
It would look something like this...

if
(System.Polite.Comparisons.My.Dearest.Computer.Kindly.Tell.Me.At.Your.Earlie
st.Convenience.Whether.The.Input.Integer.Value.Matches.One.Of.These.Extraord
inary.Values({25, 18, 87}).Thank.You(inputInt)) {
//do one thing
}
else {
//do another
}

Then here is the closest I can come up with:

if (((System.Collections.IList)((new int[]{25,18,87}))).Contains(inputInt)){
// do one thing
}
else {
// do another
}

Not the most efficient or readable way to do this, BTW.
 
Hi,

Why are you ranting him?
He has a VERY valid point, there are cases where nor if or switch cases
make senses as the possible values are several and are not consecutives.

I had one such scenario once ago and what I did was putting the values in an
array and use LastIndexOf.

If the values are dynamic you could use an ArrayList and the Contains method

Cheers,
 
Ravichandran J.V. said:
The switch()...case is a better readable alternative. If you want a
different way of finding a value it could be done through a collection
or an array. Something like

int[] arr={12,187,24};
foreach (int i in arr)

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
I've heard many times that switches are readable.

Yes compared to if .. else they are but I think that they generate too many
lines of code and programs that are too volumous are unreadable per se.

Therefore I like the approach to use a function to compare to several
different values.

But that's just my ideas.

Cheers,

mortb
 
What is the domain of the possible values? And what is the range of
true values? If the domain is reasonably small, and the range a reasonable
percentage of them you could use a table:

const bool tests[] = new bool[]
{false, false, false, true, false, true, false, false, true
//etc
// 0 1 2 3 4 5 6 7
8 // etc


if (tests[someInt))
{
// :
}


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
I have some code that needs to know if an int variable is one of
several possible values.

What is a better way of performing the test than stacking a
bunch of case: in a switch statement? I also don't want to have
a bunch of logical ORs in an if() statement (e.g., if (someInt
== 25 || someInt == 18 || someInt == 87....)

Any suggestions?

RC,

Although C# doesn't have a native 'set' data type, there is a good
one at:

http://www.codeproject.com/csharp/Sets.asp
 
Then here is the closest I can come up with:

(((System.Collections.IList)((new int[]{25,18,87}))).Contains(inputInt))
<<<

Very Very Good! Thank you.

Now all I have to do is wrap your logic in my own class, place it in my own
special polite namespace (Polite.Comparisons....) and I'm good to go!

-RC
 
Back
Top