• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch range!

 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in c# it can
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out demon spawn!
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol
nah, java rules ofcourse.
 
awais syed
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that I understand what you're trying to explain me.
 
Marilyn de Queiroz
Sheriff
Posts: 9062
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

might work.
 
awais syed
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Marilyn de Queiroz
Sheriff
Posts: 9062
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, of course I did, Jim. You're absolutely correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic