I tried to test this myself before asking on the forum but my simple code to test this didn’t seem to work.
#include <iostream>
using namespace std;
int main() {
cout << "Enter int: ";
int number;
cin >> number;
if (number==1||2||3) {
cout << "Your number was 1, 2, or 3." << endl;
}
else if (number==4||5||6) {
cout << "Your number was 4, 5, or 6." << endl;
}
else {
cout << "Your number was above 6." << endl;
}
return 0;
}
It always returns the first condition. My question is, is it even possible to have more than 2 OR conditions? Or is my syntax incorrect?
You need to code your tests differently:
The way you were doing it, the first condition was being interpreted as if it were written like this
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. Since2is a true value (as is3), the expression evaluates to true regardless of the value ofnumber.