C++ 您可以在 if 语句中使用 2 个或更多 OR 条件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8781447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:06:10  来源:igfitidea点击:

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

c++conditional-statements

提问by Ted Hopp

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?

它总是返回第一个条件。我的问题是,甚至可能有 2 个以上的 OR 条件吗?还是我的语法不正确?

回答by Ted Hopp

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 2is a true value (as is 3), the expression evaluates to true regardless of the value of number.

逻辑或运算符 ( ||) 被定义为在左侧为真或左侧为假而右侧为真时求值为真值。由于2是真值(原样3),无论 的值如何,表达式的计算结果均为真number

回答by Jerry Coffin

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 语句:

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.

请注意,就目前而言,您的代码表示 0 和所有负数都大于 6。我在第一个示例中单独保留了这一点,但在第二个示例中修复了它。

回答by schwert

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;
}

回答by AndiDog

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).

或者换句话说if(number == 1 || true || true),总是导致真。一一比较 ( number == 1 || number == 2 || number == 3) 或与范围 ( number >= 1 && number <= 3) 进行比较。

回答by Fabián Heredia Montiel

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

是的,请知道您所经历的事情发生了,因为 (2) 和 (3) 的计算结果为真。相反,你会做 number == 1 || 数字 == 2 || 数字 == 3

回答by ouah

number == 1 || 2 || 3

is equivalent to

相当于

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

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

并且由于||运算符的结果是1如果其左操作数或右操作数不同于0,则上述表达式始终计算为

1

so what you really want is the following expression

所以你真正想要的是以下表达式

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

回答by Alex

For a long list of options, you might wish to put all of your options in a static array or vector and check if they contain an option:

对于一长串选项,您可能希望将所有选项放在静态数组或向量中,并检查它们是否包含选项:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void menu_prompt(vector<int> list) {
    static vector<char> menuOptions{'P', 'A', 'M', 'S', 'L', 'Q', 'p', 'a', 'm', 's', 'l', 'q'};

    char user_input{};
    do {
        cout << "P - Print numbers" << endl;
        cout << "A - Add a number" << endl;
        cout << "M - Display mean of the numbers" << endl;
        cout << "S - Display the smallest number" << endl;
        cout << "L - Display the largest number" << endl;
        cout << "Q - Quit" << endl << endl;
        cout << "Enter your choice: " << endl;

        cin >> user_input;
    } while (std::find(menuOptions.begin(), menuOptions.end(), user_input) == menuOptions.end());
    if (user_input == 'P' || user_input == 'p') {
        //user_choice_print_numbers(list);
    }
    else if (user_input == 'A' || user_input == 'a') {
        //user_choice_add_numbers(list);
    }
    else if (user_input == 'M' || user_input == 'm') {
        //user_choice_mean_numbers(list);
    }
    else if (user_input == 'S' || user_input == 's') {
        //user_choice_smallest_numbers(list);
    }
    else if (user_input == 'L' || user_input == 'l') {
    //  user_choice_largest_number(list);
    }
    else if (user_input == 'Q' || user_input == 'q') {
        //user_choice_quit();
    }
}