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

社会主义新天地 提交于 2019-11-26 04:52:37

问题


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?


回答1:


You need to code your tests differenty:

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.




回答2:


While you can (as others have shown) re-write your tests to allow what you want, I think it's also worth considering a couple of alternatives. One would be a switch statement:

switch (number) { 
    case 1:
    case 2:
    case 3:
        cout << "Your number was 1, 2, or 3." << endl;
        break;
    case 4:
    case 5:
    case 6: 
        cout << "Your number was 4, 5, or 6." << endl;
        break;
    default:
        cout << "Your number was above 6." << endl;
}

Personally, I'd probably do something like this though:

char const *msgs[] = {
    "Your number was 1, 2, or 3.\n",
    "Your number was 4, 5, or 6.\n"
};

if (number < 1 || number > 6)
    std::cout << "Your number was outside the range 1..6.\n";
else
    std::cout << msgs[(number-1)/3];

Note that as it stands right now, your code says that 0 and all negative numbers are greater than 6. I've left this alone in the first example, but fixed it in the second.




回答3:


Try separating all of them out. I am pretty sure your syntax is incorrect

#include <iostream>
using namespace std;

int main() {
cout << "Enter int: ";
int number;
cin >> number;
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;
}
return 0;
}



回答4:


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

This code can be parenthesized like

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

or in other words if(number == 1 || true || true), always resulting in true. Compare one by one (number == 1 || number == 2 || number == 3) or with ranges (number >= 1 && number <= 3).




回答5:


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

Is my syntax incorrect?

Yes, please know that what you experienced happened because (2) and (3) evaluates to true. Instead you would do number == 1 || number == 2 || number == 3




回答6:


number == 1 || 2 || 3

is equivalent to

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

and as the the result of the || operator is 1 if either its left or its right operand is different than 0, the expression above always evaluates to

1

so what you really want is the following expression

number == 1 || number == 2 || number == 3


来源:https://stackoverflow.com/questions/8781447/can-you-use-2-or-more-or-conditions-in-an-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!