How does C++ handle &&? (Short-circuit evaluation)

C++Short CircuitingAnd OperatorLogical And

C++ Problem Overview


When encountering a (bool1 && bool2), does c++ ever attempts to check bool2 if bool1 was found false or does it ignore it the way PHP does?

Sorry if it is too basic of a question, but I really could not find a mentioning of that neither in Schildt nor on the Internet.

C++ Solutions


Solution 1 - C++

Yes, the && operator in C++ uses short-circuit evaluation so that if bool1 evaluates to false it doesn't bother evaluating bool2.

"Short-circuit evaluation" is the fancy term that you want to Google and look for in indexes.

The same happens with the || operator, if bool1 evaluates to true then the whole expression will evaluate to true, without evaluating bool2.

In case you want to evaluate all expressions anyway you can use the & and | operators.

Solution 2 - C++

C++ does use short-circuit logic, so if bool1 is false, it won't need to check bool2.

This is useful if bool2 is actually a function that returns bool, or to use a pointer:

if ( pointer && pointer->someMethod() )

without short-circuit logic, it would crash on dereferencing a NULL pointer, but with short-circuit logic, it works fine.

Solution 3 - C++

That is correct (short-cicuit behavior). But beware: short-circuiting stops if the operator invoked is not the built-in operator, but a user-defined operator&& (same with operator||).

Reference in this SO

Solution 4 - C++

The && operator short circuits in C++ - if bool1 was false in your example, bool2 wouldn't be checked/executed.

Solution 5 - C++

This is called short-circuit evaluation (Wikipedia)

The && operator is a short circuit operator in C++ and it will not evaluate bool2 if bool1 is false.

Solution 6 - C++

Short-circuit evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: for instance, when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.

In C++, both && and || operators use short-circuit evaluation.

Solution 7 - C++

What you're referring to is short circuit evaluation. I thought that it may be compiler specific, however that article I linked to shows it as language specific, and C++ does adhere. If it is indeed compiler specific, I can't imagine a compiler that wouldn't follow it. The day to day compiler I use at the moment, VS 2008, does. Basically it will follow the operator precedence, and as soon as the condition result is guaranteed,

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAleksandrs UlmeView Question on Stackoverflow
Solution 1 - C++jasonView Answer on Stackoverflow
Solution 2 - C++TimView Answer on Stackoverflow
Solution 3 - C++MatView Answer on Stackoverflow
Solution 4 - C++逆さまView Answer on Stackoverflow
Solution 5 - C++trevView Answer on Stackoverflow
Solution 6 - C++TCSGradView Answer on Stackoverflow
Solution 7 - C++TheTFoView Answer on Stackoverflow