• Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch statement  RSS feed

 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
I just finished working on an assignment (body mass index calculator) which is very simple, no problem with that. The thing is, I tried to use a switch statement to classify the categories (underweight, normal weight, overweight, etc) but I have no idea how to make it work with ranges. In VB.NET I'd use a Select Case statement (VB's switch statement) and it would accept numeric ranges like any other value, but I have no idea how to make it work with switch in Java (or perhaps I'm just too tired and my brain is malfunctioning already).

I ended up using a bunch of Ifs, but I'd really like to submit a "neater looking code" by using switch, unless of course it simply doesn't accept numeric ranges at all in its Cases and I've been wasting my time for nothing.

So for the actual question: does the switch statement accept ranges in its "Cases" at all? If so, what's the proper syntax?

Thanks in advance and sorry for my bad english.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles D. Ward wrote:So for the actual question: does the switch statement accept ranges in its "Cases" at all?


No.

If so, what's the proper syntax?


That depends on the particular situation, but for a case like this where I assume there are just a few ranges and it's simple to express them, the simplest, clearest approach would be something like:



If the bounds for the different categories follow some simple, easily describable pattern, you could do some math on the actual values to peg them to a floor or ceiling--say round up or down to the nearest 25 or whatever--and then use that value as the case for a switch, but honestly, I'd only do that as an academic exercise. The if/else may not be "elegant" but it very clearly and simply and directly expresses your rules.

Another possibility--and again, I wouldn't use it for such a simple case, but might consider it if the bmi were part of a larger system--would be to define an enum, something like:


You could then do BmiCategory category = BmiCategory.forValue(value).
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your prompt reply Jeff. I ended up using if/elseif/else as you suggested in your first example since it's just a few categories, but I'll definitely look into Enums by myself (My basic Java class just started a couple of months ago). I can already see that Enum code sample work great with some ideas I have in mind. Thanks again!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

And welcome to the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!