63

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.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Aleksandrs Ulme
  • 1,262
  • 1
  • 12
  • 21

7 Answers7

78

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.

jason
  • 236,483
  • 35
  • 423
  • 525
  • What about short circuit evaluation with or || –  Feb 10 '12 at 15:54
  • 4
    The `||` operator uses short-circuit evaluation too. The difference is that given an expression `bool1 || bool2`, when `bool1` evaluates to `false` then `bool2` will be evaluated too. – Mr. Deathless Dec 31 '14 at 09:33
  • 2
    > "_in case you want to evaluate all expressions anyway you can use the & and | operators._" Replacing logical && and ||, with bitwise operators & and | is not safe unless you're strictly using `bool` expressions. Since the logical operators can act on other types, you are not guaranteed to get reliable results. For example: `4 && 2` evaluates to `true`, while `4 & 2` evaluates to `0`. `4 || 2` evaluates to `true`, while `4 | 2` evaluates to `6`. If you want to force both expressions to be evaluated and do a logical comparison, explicitly evaluate, then compare. – Fauxcuss Feb 16 '22 at 21:17
  • It's probably clearer to follow Money47's advice too. If I saw `if (foo(x) | bar(y)`, my first thought wouldn't be "oh, `foo` and `bar` return `bool`s but the author wanted to make sure they were both called. It would be "okay, `foo` and `bar` seem to return a word of bit-flags and we're testing to see if any of the flags are set after calling both of them; this looks a little hairy". – Ben Aug 22 '22 at 03:29
41

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.

Tim
  • 8,912
  • 3
  • 39
  • 57
19

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

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    But short-circuiting is by no means limited to native types. Overloading a conversion to bool (or safe bool) instead of `operator &&` makes this work just fine. – Konrad Rudolph Mar 06 '11 at 19:42
  • Interesting. I didn't know that. Do you know the section/text from the standard that defines this? – Macke Mar 06 '11 at 20:13
  • 1
    That is correct, [reference in this SO](http://stackoverflow.com/questions/628526/is-short-circuiting-boolean-operators-mandated-in-c-c-and-evaluation-order). I'll try to rephrase my answer. – Mat Mar 06 '11 at 20:24
2

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

wkl
  • 77,184
  • 16
  • 165
  • 176
1

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.

trev
  • 669
  • 1
  • 6
  • 9
1

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.

TCSGrad
  • 11,898
  • 14
  • 49
  • 70
0

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,

TheTFo
  • 741
  • 2
  • 8
  • 25