I'm writing a program to simulate the game of blackjack, and I've been using switch to go through menus. It's been working nicely, but I'm wondering if you can extend switch to involve multiple cases. For example, there are lots of outcomes to each player. For example, blackjack, blackjack vs blackjack, player bust & dealer doesn't bust, dealer busts but player doesn't bust, both player and dealer busts, double down and busts, etc. Is there a way to use switch here comparing multiple variables, or do I have to stack the if/else-if's with multiple conditions?
3 Answers
Going off your comment to @LeoCorrea's answer, your conditions appear to be booleans.
Maybe try something like:
final int PLAYER_BUST = 1 << 0;
final int DEALER_BUST = 1 << 1;
// ...
int total = 0;
total |= player.bust ? PLAYER_BUST : 0;
total |= dealer.bust ? DEALER_BUST : 0;
switch(total) {
case 0:
// neither player nor dealer has bust
break;
case PLAYER_BUST:
// just player bust, so dealer wins
break;
case DEALER_BUST:
// just dealer bust, so player wins
break;
case PLAYER_BUST | DEALER_BUST:
// both have bust
break;
}
The 1 << x
is 2^x, and the |
is bitwise OR. My example is for n = 2, but n can be any reasonable number.
This is something like how PHP encodes its error levels. Notice how most of the levels are represented by powers of 2 (with only one '1' digit in binary).
We use |
on these levels to get the combined outcome of the n booleans. Each bit in the int total
represents one of the conditions.
Since there are 2^n possible combined outcomes of the n booleans, we can match each outcome with an integer in n bits. We can't switch on n booleans, but luckily, we can use switch
on this integer total
that represents the n booleans.

- 4,091
- 2
- 25
- 41
Unfortunately, switch
on multiple variables is not possible in Java. Apart from the bit masks @irrelephant suggested, you can try rethinking your evaluation:
I'm not completely familiar with Blackjack, but I believe that instead of setting player.busted
and similar fields, you could just set the player's score to -1
(or, if getting score of 30 is worse than score of 25, you could set it to -score
) and then simply compare the scores.
P.S. Instead of
if (player.bust == false && dealer.bust == true)
you can just type
if (!player.bust && dealer.bust)
It saves quite a lot of typing and is a lot more readable.

- 37,768
- 12
- 121
- 145
I'm not sure what language you are talking about because you didn't specify it but in Ruby the case statement (works like the switch statement)
You can do something like
case [A, B]
when [true, true] then ...
when [false, true] then ...
You can also have multiple conditions output the same output
case condition
when ("a" and "b")
# do something
when "c"
# do something
end
You're question is very vague and I think you should consider making it more specific to the language you are using.

- 19,131
- 2
- 53
- 71
-
It's java. For example: if(player.bust == false && dealer.bust == true){ player wins} else if(player.bust == true && dealer.bust == true){ } etc, etc. – Brian Dec 25 '12 at 09:33