Can you use 2 or more OR conditions in an if statement? [duplicate]

You need to code your tests differently:

if (number==1 || number==2 || number==3) {
    cout << "Your number was 1, 2, or 3." << endl;
}
else if (number==4 || number==5 || number==6) {
    cout << "Your number was 4, 5, or 6." << endl;
}
else {
    cout << "Your number was above 6." << endl;
}

The way you were doing it, the first condition was being interpreted as if it were written like this

if ( (number == 1) || 2 || 3 ) {

The logical or operator (||) is defined to evaluate to a true value if the left side is true or if the left side is false and the right side is true. Since 2 is a true value (as is 3), the expression evaluates to true regardless of the value of number.

Leave a Comment