简体   繁体   English

如何评估'if(A && B)'语句?

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

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

The question is, would the statement immediately break to else if A was FALSE. 问题是,如果A为FALSE,该语句是否会立即中断。 Would B even get evaluated? 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. 我问这个情况,当数组实际上是空的并且元素为零时,B检查数组索引的有效性就说数组[0]。 Therefore throwing a segfault because we are trying to access something that is out of bounds of the array. 因此我们尝试访问超出数组范围的内容,因此抛出了一个段错误。 Specifically 特别

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 '&&'. 如果array [0]实际得到了评估,这可能会很危险,因为它会在没有第一次检查的情况下在“&&”的左侧进行段错误。 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. 优先级告诉我,左侧肯定会优先,但它并没有告诉我,如果左边是假,它将不会评估右侧。

In C and C++, the && and || 在C和C ++中, &&|| operators "short-circuit". 运营商“短路”。 That means that they only evaluate a parameter if required. 这意味着他们只在需要时评估参数。 If the first parameter to && is false, or the first to || 如果&&的第一个参数为false,或者第一个参数为|| is true, the rest will not be evaluated. 是的,其余的不会被评估。

The code you posted is safe, though I question why you'd include an empty else block. 您发布的代码是安全的,但我质疑您为什么要包含一个空的else块。

You are asking about the && operator, not the if statement. 您询问的是&&运算符,而不是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 && x will 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 ,然后终止,因为没有办法的,无论是什么来的第二个参数的表达式计算非零&&

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. 当一些陈述具有副作用时,这非常重要。

for logical && both the parameters must be true , then it ll be entered in if {} clock otherwise it ll execute else {}. 对于logical &&这两个参数必须为true,然后输入if {}时钟,否则它将执行else {}。 for logical || 对于逻辑|| one of parameter or condition is true is sufficient to execute if {}. 如果{},则参数或条件之一为真就足以执行。

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

yes, if( (A) && (B) ) will fail on the first clause, if (A) evaluates false. 是的,如果((A)&&(B))将在第一个子句上失败,如果(A)评估为假。

this applies to any language btw, not just C derivatives. 这适用于任何语言btw,而不仅仅是C衍生物。 For threaded and parallel processing this is a different story ;) 对于线程和并行处理,这是一个不同的故事;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM