Search code examples
c++short-circuiting

C++ short-circuiting of booleans


I'm new to c++ and am curious how the compiler handles lazy evaluation of booleans. For example,

if(A == 1 || B == 2){...}

If A does equal 1, is the B==2 part ever evaluated?


Solution

  • No, the B==2 part is not evaluated. This is called short-circuit evaluation.

    Edit: As Robert C. Cartaino rightly points out, if the logical operator is overloaded, short-circuit evaluation does not take place (that having been said, why someone would overload a logical operator is beyond me).