Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR operator in switch-case?

Let's take a simple switch-case that looks like:

@Override public void onClick(View v) {     switch (v.getId()) {         case R.id.someValue :         case R.id.someOtherValue:             // do stuff             break;     } } 

I wonder why it is not allowed to use the || operator? Like

switch (v.getId()) {     case R.id.someValue || R.id.someOtherValue:         // do stuff         break; } 

The switch-case construct is pretty similar to an if-else statement, you can use the OR operator in an if however. What are the backgrounds for a switch-case to not accept this operator?

like image 386
Droidman Avatar asked Aug 23 '13 22:08

Droidman


People also ask

Can I use or operator in switch-case?

The switch-case construct is pretty similar to an if-else statement, you can use the OR operator in an if however.

Can I use && in a switch statement?

The simple answer is No. You cant use it like that.

Can you use || IN case statement Ruby?

If you are eager to know how to use an OR condition in a Ruby switch case: So, in a case statement, a , is the equivalent of || in an if statement.

Can we use or condition in switch-case in Java?

No. It's not possible because a case must be a constant expression.


1 Answers

dude do like this

    case R.id.someValue :     case R.id.someOtherValue :        //do stuff 

This is same as using OR operator between two values Because of this case operator isn't there in switch case

like image 98
spt025 Avatar answered Oct 01 '22 10:10

spt025