在C ++中在Switch-Case中执行OR [英] Do an OR in Switch-Case in C++

查看:255
本文介绍了在C ++中在Switch-Case中执行OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中做到这一点?例如,如果用户按ESC或'q'或'Q',我将尝试触发程序退出。

How would you do this in C++? For example I'm trying to trigger a program exit both if the user presses ESC or 'q' or 'Q'.

我试图查找它,但是我在C ++中没有找到它的语法。我知道如何做它与if-else,但是它是可能的开关case?当然,我可以只是做一个函数,并从两个单独的情况下调用它,但有一个方法来做它只是通过组合case语句?

I've tried looking for it, but I found no syntax for it in C++. I know how to do it with if-else, but is it possible with switch - case? Of course I can just make a function and call it from two separate case, but is there a way to do it just by combined case statement?

例如,这就是我'm寻找(当然不工作):

For example that's what I'm looking for (of course not working):

void keyboard( unsigned char key, int x, int y )
{
    switch( key )
    {
        case ( 27 || 'q' || 'Q' ):
            exit( 0 );
            break;

        case 'a': ...

                case 'b': ...
    }
}


推荐答案

案例没有中断:

case  27: //could be 27
case 'q': //could be 27 or 'q'
case 'Q': //could be 27, 'q', or 'Q'
    exit(0);
    break;

这篇关于在C ++中在Switch-Case中执行OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆