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
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, 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.
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth