Don't
Don't

Reputation: 51

Java switch statements

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

switch(a && b){

case 1:

//

case 2:

//

}

Upvotes: 0

Views: 128

Answers (2)

Ajinkya Patil
Ajinkya Patil

Reputation: 741

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:

    //

    }

Upvotes: 0

Eran
Eran

Reputation: 393821

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:   
}

Upvotes: 6

Related Questions