C++ IF 语句中的多个 OR 或 AND 条件

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

Multiple OR or AND conditions in IF statement

c++if-statementoperatorslogical-operators

提问by user3663688

I am having a basic doubt regarding IF statement. Let's say I want to match string SUN with a character array(size 3).

我对 IF 语句有一个基本的疑问。假设我想将字符串 SUN 与字符数组(大小为 3)匹配。

if(arr[0]!='S' || arr[1]!='U' || arr[2]!='N')

cout << "no";

else

cout<< "yes";

Are all conditions checked in If statement or does it return true on first mismatch?

是否在 If 语句中检查了所有条件,还是在第一次不匹配时返回 true?

If all conditions are checked, will the the order of checking be from right to left?

如果所有条件都检查了,检查的顺序是从右到左吗?

回答by Vlad from Moscow

According to the C++ Standard

根据 C++ 标准

1 The && operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

1 && 运算符从左到右分组。操作数都根据上下文转换为 bool(第 4 条)。如果两个操作数都为真,则结果为真,否则为假。与 & 不同,&& 保证从左到右的计算:如果第一个操作数为假,则不计算第二个操作数。

and

1 The || operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.

1 || 运算符组从左到右。操作数都根据上下文转换为 bool(第 4 条)。如果其任一操作数为真,则返回真,否则返回假。不像 |, || 保证从左到右的评估;此外,如果第一个操作数的计算结果为真,则不会计算第二个操作数。

回答by Alex Shtof

It returns "true" on the first mismatch. Operators in both C and C++ "short-circuit". That is, an OR operator will not evaluate its right side when its left side returned true. The AND operator will not evaluate its right side if its left side returned false.

它在第一次不匹配时返回“true”。C 和 C++ 中的运算符“短路”。也就是说,当 OR 运算符的左侧返回 true 时,它​​不会评估其右侧。如果其左侧返回 false,AND 运算符将不会评估其右侧。

回答by Giulio

The if-statement you wrote execute cout << "no";if AT LEAST one of the conditions is satisfied.

如果cout << "no";至少满足其中一个条件,则执行您编写的 if 语句。

In this case, if your input is "GUN", the program checks if:

在这种情况下,如果您的输入是“GUN”,程序会检查:

  • 'G' != 'S' -> YES
  • 'U' != 'U' -> NO
  • 'N' != 'N' -> NO
  • 'G' != 'S' -> 是
  • 'U' != 'U' -> 否
  • 'N' != 'N' -> 否

As the first condition is satisfied, the program will execute cout << "no";.

当第一个条件满足时,程序将执行cout << "no";

If the if-statement was:

如果 if 语句是:

if(arr[0]!='S' && arr[1]!='U' && arr[2]!='N')
  cout << "no";
else
  cout<< "yes";

To execute cout << "no";ALL the conditions had to be satisfied.

要执行cout << "no";所有条件,必须满足。

For "GUN" it would be like:

对于“GUN”,它会是这样的:

  • 'G' != 'S' -> YES
  • 'U' != 'U' -> NO
  • 'N' != 'N' -> NO
  • 'G' != 'S' -> 是
  • 'U' != 'U' -> 否
  • 'N' != 'N' -> 否

As there are two NO, the program would execute cout<< "yes";.

由于有两个 NO,程序将执行cout<< "yes";.

So:

所以:

  • || requires just one condition to be satisfied
  • && requires ALL conditions to be satisfied
  • || 只需要满足一个条件
  • && 要求满足所有条件