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

标签 c++ if-statement operators logical-operators

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

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

cout << "no";

else

cout<< "yes";

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

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

最佳答案

根据 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 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.

关于c++ - IF 语句中的多个 OR 或 AND 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24366407/

相关文章:

c++ - MPFR C++ 像整数一样打印 float

c++ - 使用 directx 11 时出现 Unresolved 链接错误

php - 了解 PHP 赋值

javascript - javascript 如何检查字符串中是否至少有 6 个字符?

Python 运算符 pymysql

c++ - 解释一下区别:

c++ - 将流运算符 >> 评估为 bool 值

c++ - 在 C++ 中调用私有(private)方法

java - 处理 - 根据 mousePressed 播放不同的音频

python-3.x - Django 休息 : AssertionError: Cannot combine a unique query with a non-unique query