你能在if语句中使用2个或更多OR条件吗?

时间:2012-01-08 21:22:41

标签: c++ conditional-statements

我在论坛上询问之前试图自己测试一下,但我测试这个的简单代码似乎不起作用。

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

它总是返回第一个条件。我的问题是,是否有可能超过2个OR条件?或者我的语法不正确?

9 个答案:

答案 0 :(得分:11)

您需要对测试进行不同的编码:

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

你这样做的方式,第一个条件被解释为好像是这样写的

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

如果左侧为真或左侧为假且右侧为真,则定义逻辑或运算符(||)以评估为真值。由于2是真值(与3一样),因此无论number的值如何,表达式的计算结果都为真。

答案 1 :(得分:3)

虽然你可以(正如其他人所示)重新编写测试以允许你想要的东西,但我认为它也值得考虑一些替代方案。一个是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;
}

就个人而言,我可能会做这样的事情:

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

请注意,正如它现在所示,您的代码表示0和所有负数大于6.我在第一个示例中单独留下了这个,但在第二个示例中将其修复了。

答案 2 :(得分:1)

尝试将所有这些分开。我很确定你的语法不正确

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

答案 3 :(得分:1)

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

此代码可以像

一样括起来
if ((number==1) || (2) || (3))

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

答案 4 :(得分:0)

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

我的语法不正确吗?

是的,请知道您所经历的事情是因为(2)和(3)评估为真。相反,你会做数字== 1 || number == 2 || number == 3

答案 5 :(得分:0)

number == 1 || 2 || 3

相当于

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

并且||运算符的结果为1,如果其左侧或右侧操作数与0不同,则上面的表达式始终求值为

1

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

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

答案 6 :(得分:0)

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

#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();
    }
}

答案 7 :(得分:0)

您必须像这样比较

if (number == 1 || number ==2 || number == 3){...} 每次比较时都必须输入 number ==


专业提示 始终初始化变量int number{0}; 不要在未初始化的情况下声明var。

答案 8 :(得分:0)

您似乎想在此链接 or

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

上面应该写成

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

或者,通过检查 number 是否在 [1,3] 范围内:

if (number>=1 && number<=3)

如果您想链接多个 or 范围),您可以使用折叠表达式 (C++17) 创建一个辅助模板函数:< /p>

示例:

#include <functional> // std::equal_to
#include <iostream>

// matching exactly two operands
template<class T, class BinaryPredicate>
inline constexpr bool chained_or(const T& v1, BinaryPredicate p, const T& v2)
{
    return p(v1, v2);
}

// matching three or more operands
template<class T, class BinaryPredicate, class... Ts>
inline constexpr bool chained_or(const T& v1, BinaryPredicate p, const T& v2,
                                 const Ts&... vs)
{
    return p(v1, v2) || (chained_or(v1, p, vs) || ...); // fold expression
}

int main() {
    // check if 6  is equal to any of the listed numbers
    if constexpr (chained_or(6, std::equal_to<int>(), 1,3,4,6,9)) {
        std::cout << "6 - true\n";
    }

    // check if 7 is any of the listed numbers
    if constexpr(chained_or(7, std::equal_to<int>(), 1,3,4,6,9)) {
        std::cout << "7 - true\n";
    }
}

chained_or 被实例化时,折叠表达式将展开为:

(6 == 1 || 6 == 3 || 6 == 4 || 6 == 6 || 6 == 9) // true

(7 == 1 || 7 == 3 || 7 == 4 || 7 == 6 || 7 == 9) // false