C++ 如何在交换机状态中设置逻辑或案例部分?

C++ 如何在交换机状态中设置逻辑或案例部分?,c++,switch-statement,logical-operators,C++,Switch Statement,Logical Operators,如果您有一个switch语句,并且希望在值为一个值或另一个值时运行某些代码,那么如何执行该操作?以下代码始终使用默认情况 #include <iostream> using namespace std; int main() { int x = 5; switch(x) { case 5 || 2: cout << "here I am" << endl; break;

如果您有一个switch语句,并且希望在值为一个值或另一个值时运行某些代码,那么如何执行该操作?以下代码始终使用默认情况

#include <iostream>
using namespace std;

int main()
{
    int x = 5;
    switch(x)
    {
        case 5 || 2:
            cout << "here I am" << endl;
            break;
        default:
            cout << "no go" << endl;
    }

    return 0;
}
#包括
使用名称空间std;
int main()
{
int x=5;
开关(x)
{
案例5 | | 2:

不能让
开关
通过

switch(x)
{
    case 2:
    case 5:
        cout << "here I am" << endl;
        break;
    default:
        cout << "no go" << endl;
}
开关(x)
{
案例2:
案例5:
不能使它通过:

int main()
{
    int x = 5;
    switch(x)
    {
        case 5:
        // there's no break statement here,
        // so we fall through to 2
        case 2:
            cout << "here I am" << endl;
            break;
        default:
            cout << "no go" << endl;
    }

    return 0;
}
intmain()
{
int x=5;
开关(x)
{
案例5:
//这里没有中断声明,
//因此,我们的答案是2
案例2:
不能像这样:

switch (x)
{
case 5:
case 2:
    cout << "here I am" << endl;
    break;
}
开关(x)
{
案例5:
案例2:

cout关于switch/case及其在C/C++和其他一些关于标签的信息之间的细微差别(ABCD:)你可能想知道。

@Celeritas,我从来没有说过它的计算结果是
2
,它是逻辑OR。对不起,我的意思是5 | | 2的计算结果是1。但是在二进制101或11=111中,它是7,不是1。对吗?@Celeritas,它是逻辑OR,不是按位的。请查看我答案中的链接。
case2:
case5:
   //do things
   break;