0

Was just wondering if I could do something like this in java with switch statements?

switch(a && b){

case 1:

//

case 2:

//

}
3
  • 2
    No you can't switch based on a boolean value. Commented May 2, 2016 at 8:47
  • 2
    so as per your username, when I say 'Don't try it yourself', you really won't try it. Commented May 2, 2016 at 8:48
  • What are a and b and why would you expect && to produce either 1 or 2 when this operator returns a boolean? Commented May 2, 2016 at 8:49

2 Answers 2

6

Sure you can, but not with logical AND (&&). You probably meant to use bit-wise AND (as your case clauses suggest) :

switch(a & b) {
case 1: 
case 2:   
}
Sign up to request clarification or add additional context in comments.

Comments

0

What is the data type of a & b ? If it is boolean then it won't work, switch works with int. If they are int then you must use single & operation and not a &&

 public static void main(String[] args) {
    int a,b;
    a=5;
    b=2;
    switch(a & b){

    case 1:

    //

    case 2:

    //

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.