wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes switch range! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "switch range!" Watch "switch range!" New topic
Author

switch range!

Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Hi people,
I want to know if there is a way to do something like this with a switch.
If ( i>=10 && i<=20)
then ...
else if (i>=21 && i<=30)
then ...
else if (i>=31 && i<=40)
then ...
else ...
With a switch it should be something like this
switch (i)
case ( i>=10 && i<=20):...
case ( i>=21 && i<=30):...
case ( i>=31 && i<=40):...
It doesn't compile since the expression returns a boolean and we need an int (or something "smaller").
In ADA we may do
case i is
when 10..20=> ...
when 11..30=> ...
when 31..40=> ...
when ...
Is there any equivalent in Java or the only way to achieve it is to use the condition if???
And if there is no way, do you have an idea why are we forced to use an int with a switch? I think that a boolean would have been very helpfull, no?
[ June 18, 2002: Message edited by: Younes Essouabni ]

Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
A case must be reconcilable as an integer. It would be nice, at times, if a switch could handle lots of different data types, but it can't.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
in c# it can
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Out demon spawn!
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Originally posted by Dirk Schreckmann:
Out demon spawn!

At least it wasn't from me, Roy!!
Thx Dirk and Roy!
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
lol
nah, java rules ofcourse.
awais syed
Greenhorn

Joined: Mar 17, 2002
Posts: 7
hi all,
in java switch takes a value that may be byte,short,char and int as argument
and up to my knowledge at compile time the compiler construct a jump table on the basis of case labels and this make the excecuting of switch statement fast at runtime.
and there are situations where use of switch is far effective than if structure.

as per your question you use switch rather then if structure using following code.
we write program using

the result od using if structure and switch are same.
[ edited to format code using the [code] and [/code] UBB tags -ds ]
[ June 20, 2002: Message edited by: Dirk Schreckmann ]

awais
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
I'm not sure that I understand what you're trying to explain me.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10

might work.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
awais syed
Greenhorn

Joined: Mar 17, 2002
Posts: 7
hi,Younes Essouabni
it is explanation part of your question that
"Is there any equivalent in Java or the only way to achieve it is to use the condition if???"
the second part of your question
"And if there is no way, do you have an idea why are we forced to use an int with a switch? I think that a boolean would have been very helpfull, no? ".
the reason of that i give last time that at compile time compiler create a jump table on basis of case label that is not possible if the boolean expresion is used as a case label.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Ok, I understand what you mean, but doing it this way
I think it's not really a good solution, since the value I put were just for example. I could have put 10,32,48,23,54,... and the code won't work anymore (unless you find some hard algorithme )
Anyway thx awais and thx Marilyn
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Also, I think Marilyn meant (i / 10) rather than (i % 10). But as Younes notes, this still won't work for intervals that aren't nice integral multiples of some common factor. Ah well...


"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Yes, of course I did, Jim. You're absolutely correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch range!