我对 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/