I want to use a switch statement, and my condition fot the if is:
if (userInput%2 == 0 || userInput != 0)
Can I get two cases from this code to execute different actions for userInput == 0 and different one for userInput == 0
case ?:
case ?:
You cannot, because the value sets satisfying the two conditions overlap. Specifically, all even numbers satisfy both parts of your conditions. That is why you cannot perform different actions without deciding first which part of the condition takes precedence.
You can play a little trick with fall-through inside the switch statement, like this:
switch(userInput%2) {
case 0:
// Do things for the case when userInput%2 == 0
...
// Note: missing "break" here is intentional
default:
if (userInput == 0) break;
// Do things for the case when user input is non-zero
// This code will execute when userInput is even, too,
// because of the missing break.
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With