• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Switch

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 65413
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:-
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Marshal
Posts: 65413
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clever idea. I am not sure that is what an enum was intended for, however.
 
I was born with webbed fish toes. This tiny ad is my only friend:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!