aspose file tools*
The moose likes Beginning Java and the fly likes checking among a range of values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "checking among a range of values" Watch "checking among a range of values" New topic
Author

checking among a range of values

shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Hi,
i remember that in some language , we have a feature that is somewhat similar to this :
int i;
if (i IN (2,5,7, 8,11,13, 12,17))
{
// DO SOMETHING
}
I have used the syntax as a help of course. This is not the way it is in C,C++.
Is there anything similar in Java? Or, do have to cascade in
a switch or else use multiple | | (conditional OR) ?
Shree


Regards,<BR>Shree
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Shree,
You could use a set. Here is one example:

Hope this helps.
Stephanie
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 570
    
    7

Shree,
another way to do this is a compound switch statement:class
<pre>
TestSwitch {
public static void main ( String[] args ) {

int i = 2;
switch ( i ) {
case 2:
case 5:
case 7:
case 8:
case 11:
case 13:
case 12:
case 17:
System.out.println("Value Found");
break;
default:
System.out.println("Value not found");
}
}
}
</pre>
But unless i were writting imbedded code (very small) or
extreamly time critical Stephanie's method is far better.
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Thanks,

By the way, steve, does a switch become a 'compound switch' when you cascade more than one case statement?

Shree
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 570
    
    7

Shree,
Compound Switch {sounds good, but i have never heard of such
a creature - but then there an awful lot i don't know}
Steve
Apu Nahasapeemapetilon
Ranch Hand

Joined: Sep 06, 2000
Posts: 51
Steve,
You wrote;
Shree,
another way to do this is a compound switch statement:class

He asking you what you meant by that. I think you mean a cascading switch, very populat in C++.
Shree,
Steve is right, Stephs concept is a little better, both require hard coding of the data, something that gives Apu a brain freeze faster than engulfing squishees, but...
A compound switch would indeed imply a switch statement inside another's case. Though, I am having trouble try to come up with a good reason to do this, wait...

something like that, though there may be more elegant ways.
 
wood burning stoves
 
subject: checking among a range of values