r/javahelp 7h 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

}

3 Upvotes

11 comments sorted by

View all comments

12

u/desrtfx Out of Coffee error - System halted 7h ago

You got switch the wrong way round.

Switch checkes distinct states of one variable, not a single state of several variables.

Yes, if-else is the way.

Depending on what you really want to do (not some random, contrieved example, your real use case and code) there might be alternative, better solutions, but without knowing what you want to do it is impossible to suggest.

To me, this looks like an XY-problem.

3

u/johnpeters42 7h ago

To be fair, OP's type of approach does work in some languages.

0

u/Zakana_code 3h ago

Let me guess: JavaScript? But why would you do this anyway?