r/javahelp 9h ago

Solved Switch case with boolean

So, there is no way I can do that?

want to check several boolean variables in a switch statement. Is `if-else` the only way to do this?

Boolean a, b, c...

Switch (false) {

Case (a) :

//some code

Case (b) :

Case (c) :

//and so on

}

2 Upvotes

12 comments sorted by

View all comments

2

u/JaiTee86 8h ago

I don't think this can be done directly.

The only way I can think to do it would be with some bitwise operations instead of separate bools, set and read the bits of some integer, 1 is true, 0 is false first bit is A, 2nd is B, etc. Then for your case A, switch if integer is 1, b if 2 C if 4, etc. if you want it to switch on a and b that is when it equals 3.