Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

checking among a range of values

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shree,
You could use a set. Here is one example:

Hope this helps.
Stephanie
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

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

Shree
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
    Bookmark Topic Watch Topic
  • New Topic