• Post Reply Bookmark Topic Watch Topic
  • New Topic

switch: how to use one "case" for multiple/range option  RSS feed

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I would like to know whats the syntax for using the case for a range or multiple options.. example

How to implement the OR part in the case.
Thanks in advance
GUL
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ January 02, 2004: Message edited by: Michael Morris ]
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Once a case statement is found, the code will continue to run until it hits a break statement. So if you have code that needed to be run for both 2 and 3, AND code that only runs for 2, you could do this;
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like on case to be executed for multiple options thats y i used to OR word. i have a variable range of almost 30 out of which i want to run a common method for almost half of them.
So it will be much easier for me to write only 10-15 cases which cover all the 30 options.
Thanks
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark that worked.
GUL
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have multiple cases in which one or more has a statement without a break, you should always place a //falls thru comment before the next case statement for the poor schmuck that has to maintain the code when you are gone. When you have very complex switch/case constructs you have probably overlooked a better design solution. Problems resulting from switch statements can be very difficult to detect and it is easy to overlook the need for a break in a particular case.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael - good point! Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!