Reputation:
how do I simplify this while loop? It doesn't seem to work when I put != 2 && 3 && 12.
Thank you
while (sum(diceRoll) != 2 && sum(diceRoll) !=3 && sum(diceRoll) !=12)
Upvotes: 2
Views: 41
Reputation: 51988
Using the %in%
operator, you could replace
while (sum(diceRoll) != 2 && sum(diceRoll) !=3 && sum(diceRoll) !=12)
by
while (!(sum(diceRoll) %in% c(2,3,12)))
The problem with your proposed solution is that 2 && 3 && 12
evaluates to TRUE
so
while (sum(diceRoll) != 2 && 3 && 12)
parses as
while (sum(diceRoll) != TRUE)
which is functionally equivalent to
while (sum(diceRoll) != 1)
which is clearly not the intended meaning.
Upvotes: 2