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

后端 未结 5 492
野性不改
野性不改 2020-12-14 18:47
if( (A) && (B) )
{
  //do something
}
else
  //do something else

The question is, would the statement immediately break to else if A was FA

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 19:10

    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.

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

提交回复
热议问题