Can you use an OR in a switch?

Viewed 94

Can you use an OR operator in a switch case? I was trying to remove case sensitivity from a user input check (so there may be a better way to do this altogether, I'm a beginner after all), and this of course resulted in doubling my switch case (not including the default case). Would it be better to use an if/else at this point, or is there a way to check for different conditions in a case?

My code, if it helps:

    case 'Y':
    case 'y':
        cout << "Good, we will check later to ensure your opinion is unchanged." << endl;
        break;
    case 'N':
    case 'n':
        cout << "Open your window tonight, unlock the door, take sleep medication, and ignore any noises in your room." << endl;
        break;
    default:
        cout << "Not an answer... think you can play games with me? I'll give you worse than the lemon haters, you'll be better off killing yourself before I take matters into my own hands." << endl;
3 Answers

If you do not plan to distinguish whether letters are lowercase or uppercase, then the best solution would be to "convert" your switch variable to lower/uppercase. By that you will get rid of unnecessary cases and save some time in future while implementing new ones.

Depending on your decision, you may choose std::tolower or std::toupper:

switch (std::tolower(x)) {
    case 'y':
        std::cout << "answer1" << std::endl;
        break;
    case 'n':
        std::cout << "answer2" << std::endl;
        break;
    default:
        std::cout << "answer3" << std::endl; 
        break;
}

Answering your question about usage of OR statement - it will not work well with char. All characters are defined with some integers (values from ASCII table), which means that e.g. y has the value 121, while Y has 89.

Let's take a look at expression case 'y' || 'Y': the value returned from 'y' || 'Y' would actually be 1. How is that possible? OR statement returns 1 if at least one of arguments is not 0 and 0 otherwise. This is how the value is calculated:

case 'y' ||  'Y'    =>    case 121 || 89    =>    case 1

Sadly, neither y nor Y is defined as 1 in ASCII, so switch will use default case with that input.

you can use it but it will not work, why? let me explain

when pass variable to switch() c++ will compare its value and type with each case, when you use "or" is a logical operator that means the result of the comparison will be a boolean value, check this example:

int main()
{
char input = 'R';

switch(input){
    case 'R' || 'r' : // the 'or' opreator will return true wich is not equal to R   
        cout<<"wow!!!";
        break;
    default:
        cout<<"meh";
}


return 0;
}

solution: you can use if/else statement

 if(input=='Y' || input =='y'){
    cout<<"Good, we will check later to ensure your opinion is unchanged." << endl;
}else if(input=='N' || input =='n'){
    cout<<"Open your window tonight, unlock the door, take sleep medication, and ignore any noises in your room." << endl;
}else{
    cout << "Not an answer... think you can play games with me? I'll give you worse than the lemon haters, you'll be better off killing yourself before I take matters into my own hands." << endl;
}

**or you can use tolower() to force the input to be lower case **

You can't use OR in a switch case, since it checks equality of the input and the case, so when using || evaluates to either true or false, which likely won't be equal to the input (for example, you check if a number, like 5, is equal to false). @Mohammed Khaled Gave a good explanation. What you can do is simply make an if/else chain (where you can use ||), or copy and paste the code from the switch cases.

 int a = some_value;

switch(a) {
    case 5:
        do_something_1;
    case 6:
        do_something_1;
    case 7:
        do_something_2;
}
Related