Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Beginning Java
Switch
Manish Pamnani
Ranch Hand
Posts: 57
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can't switch have a range as a case just like we do in if?
Like for example we can do if(i > 5 && i < 1)
can we do similiar things in switch?
If yes, then how?
Campbell Ritchie
Marshal
Posts: 75724
354
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Manish Pamnani wrote:
Can't switch have a range as a case just like we do in if? . . .
Afraid not Sorry. Try
if‑else if
:-
if (score < 0 || score > 100) { throw new IllegalArgumentException(...); } if (score < 20) { grade = Grade.U; } else if (grade < 40) { grade = Grade.F; } else if (grade < 50) { grade = Grade.D; } else if (grade < 60) { grade = Grade.C: } else if (grade < 70) { grade = Grade.B; { else if (grade < 80) { grade = Grade.A; } else { grade = Grade.A_PLUS; }
Michael Krimgen
Ranch Hand
Posts: 54
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You could use a switch with an enum. This could be an option if you want to reuse the conditions and keep the switch short.
Here is a simplified example. Please note that I do not advocate this
pattern
, but it might be appropriate in some cases.
public class Switch { public static void main(String[] argv) { int i = 5; switch(Compare.getByValue(i)) { case RANGE_1_5: System.out.println("one to five"); break; case RANGE_8_12: System.out.println("eight to twelve"); break; default: System.out.println("default value"); } } private enum Compare { //do not add overlapping values RANGE_1_5(1,5), RANGE_8_12(8,12), DEFAULT(0,-1); int startRange, endRange; Compare(int start, int end) { startRange = start; endRange = end; } public static Compare getByValue(int i) { for(Compare c : Compare.values()) { if( i <= c.endRange && i>= c.startRange) { return c; } } return DEFAULT; } } }
Campbell Ritchie
Marshal
Posts: 75724
354
posted 3 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Clever idea. I am not sure that is what an
enum
was intended for, however.
Is that a spider in your hair? Here, threaten it with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Prompt, if/else statements, and switch statement
about "for" and "if-else" ??
Selecting code without switch(...)
Questions about switch
RockPaperScissorsLizardSpock
More...