C语言 如何编写带有多个 || 的 if 语句 和&&在C中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23719901/
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-09-02 11:05:40  来源:igfitidea点击:

How to write an if statement with multiple || and && in C?

cif-statementmultiple-conditions

提问by user3649227

What is a concise way to write an if statement with more than many || and && in C?

用多个 || 编写 if 语句的简洁方法是什么?和&&在C中?

I want to only execute a printf statement if a either 1,2,4 or 6 AND b = 8 and c = 10, can I put all these conditions into the same if statement?

我只想在 a 1,2,4 或 6 AND b = 8 and c = 10 时执行 printf 语句,我可以将所有这些条件放入同一个 if 语句中吗?

eg. can I write something like:

例如。我可以写一些类似的东西:

if ((a = 1||2||4||6) && b == 8 && c == 10)

//do something

This doesn't seem to work...

这似乎不起作用...

回答by Edward Clements

if ((a == 1 || a == 2 || a == 4 || a == 6) && b == 8 && c == 10)

回答by Makoto

It might be better to write this with a switchstatement inside of an ifinstead.

最好switch在 an中用一个语句来写这个if

if(b == 8 && c == 10) {
    switch(a) {
        case 1:
        case 2:
        case 4:
        case 6:
            printf("value works\n");
    }
}