C++ 如何评估“if (A && B)”语句?

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

How an 'if (A && B)' statement is evaluated?

c++

提问by BuckFilledPlatypus

if( (A) && (B) )
{
  //do something
}
else
  //do something else

The question is, would the statement immediately break to else if A was FALSE. Would B even get evaluated?

问题是,如果 A 为 FALSE,该语句是否会立即中断为 else。B 甚至会被评估吗?

I ask this in the case that B checking the validity of an array index say array[0] when the array is actually empty and has zero elements. Therefore throwing a segfault because we are trying to access something that is out of bounds of the array. Specifically

在 B 检查数组索引的有效性的情况下,我问这个问题,当数组实际上是空的并且有零个元素时说 array[0] 。因此抛出段错误,因为我们试图访问超出数组范围的内容。具体来说

if( (array.GetElements() > 0) && (array[0]))
  array[0]->doSomething();
else
  //do nothing and return

This may be dangerous if array[0] actually gets evaluated because it segfaults without the first check to the left of the '&&'. Precedence tells me that the left side will definitely take precedence but it doesn't tell me that it won't evaluate the right side if the left is FALSE.

如果 array[0] 实际被评估,这可能是危险的,因为它在没有对“&&”左侧的第一次检查的情况下发生了段错误。优先级告诉我左侧肯定会优先,但它并没有告诉我如果左侧为 FALSE,它不会评估右侧。

回答by John Millikin

In C and C++, the &&and ||operators "short-circuit". That means that they only evaluate a parameter if required. If the first parameter to &&is false, or the first to ||is true, the rest will not be evaluated.

在 C 和 C++ 中,&&||运算符“短路”。这意味着它们仅在需要时评估参数。如果第一个参数 to&&为 false,或者第一个 to||为 true,则不会评估其余参数。

The code you posted is safe, though I question why you'd include an empty elseblock.

您发布的代码是安全的,但我质疑您为什么要包含一个空else块。

回答by strager

You are asking about the &&operator, not the ifstatement.

您是在询问&&运算符,而不是if语句。

&&short-circuits, meaning that if while working it meets a condition which results in only one answer, it will stop working and use that answer.

&&短路,这意味着如果在工作时它满足导致只有一个答案的条件,它将停止工作并使用该答案。

So, 0 && xwill execute 0, then terminate because there is no way for the expression to evaluate non-zero regardless of what is the second parameter to &&.

因此,0 && x将执行0,然后终止,因为无论 的第二个参数是什么,表达式都无法计算非零值&&

回答by Gavin H

Yes, it is called Short-circuit Evaluation.

是的,它被称为短路评估

If the validity of the boolean statement can be assured after part of the statement, the rest is not evaluated.

如果布尔语句的有效性在语句的一部分之后得到保证,则不评估其余部分。

This is very important when some of the statements have side-effects.

当某些语句具有副作用时,这非常重要。

回答by Prasad Shinde

for logical && both the parameters must be true , then it ll be entered in if {} clock otherwise it ll execute else {}. for logical || one of parameter or condition is true is sufficient to execute if {}.

对于逻辑 && 两个参数都必须是 true ,然后它会被输入 if {} clock 否则它会执行 else {}。对于逻辑 || 参数或条件之一为真就足以执行 if {}。

if( (A) && (B) ){
     //if A and B both are true
}else{
}
if( (A) ||(B) ){
     //if A or B is true 
}else{
}

回答by michael pearson

yes, if( (A) && (B) ) will fail on the first clause, if (A) evaluates false.

是的, if( (A) && (B) ) 将在第一个子句上失败,如果 (A) 评估为假。

this applies to any language btw, not just C derivatives. For threaded and parallel processing this is a different story ;)

顺便说一句,这适用于任何语言,而不仅仅是 C 衍生物。对于线程和并行处理,这是一个不同的故事;)