Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9414

Can I use OR statements in Java switches?

I'm wondering if I can use or in switch-case in Java?

Example

switch (value)
{
   case 0:
      do();
      break;

   case 2 OR 3 
      do2();
      break;
} 

Upvotes: 5

Views: 387

Answers (1)

aioobe
aioobe

Reputation: 421090

There is no "or" operator in the case expressions in the Java language. You can however let one case "fall through" to another by omitting the break statement:

switch (value)
{
   case 0:
      do();
      break;

   case 2:  // fall through
   case 3:
      do2();
      break;
} 

Upvotes: 17

Related Questions