简体   繁体   中英

Evaluation order of operands of logical operators such as &&

I was thinking about a situation like this :

if( A && B)
{
    doSomething();
}

Considering I am using C++, what is evaluated first ? A or B ?

And, let's imagine A is evaluated first, if A is false , will the if statement try to evaluate B before going out of the if ?

Is the comportement the same is most other programming languages ?

&& and || are typically implemented as short-circuit operators. If A is false , B does not need to be evaluated and will (in most languages) not be. So A will be evaluated first and depending on its value, B may or may not be evaluated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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