1

This question already has an answer here:

I've been reading the C++ for dummies book and I'm loving it! I'm on the section where he is talking about short circuit evaluation. Here is my understanding of short circuit evaluation.

Short circuit evaluation is only applied to bitwise operators, for example AND && and OR ||. So if condition1 is not true then the whole statement is not true!

I've been trying to get my head around this concept by using the following code.

#include <iostream>
using namespace std;

int main()
{
    bool condition1 = true;
    bool condition2 = false;

    if (condition1 && condition2)
    {
        cout << "The statement is true!";
    }

    else
    {
        cout << "The statement is false.";
    }
}

But I'm returned with the statement being false have I grasped this concept the wrong way?

marked as duplicate by Blrfl, gnat, Bart van Ingen Schenau, Christophe, Greg Burghardt Jul 17 '16 at 19:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    Side note: && and || are logical operators and short-circuit. The bitwise operators & and | are guaranteed to never short-circuit even for values with all bits cleared or set. – Peter G. Jul 16 '16 at 16:58
4

Short circuit evaluation isn't about true or false. It's about not evaluating part of an expression if you can predict the result without it. Since false && whatever() returns false regardless of whatever() you don't need to call whatever().

Your code doesn't demonstrate this at all. You simply can't demonstrate this with true and false. You need a side effect.

Say whatever() throws an exception. Now we'll know if it was called and if it wasn't. You could also have it display on the console.

Short circuit evaluation can be thought of two ways. An efficient way to avoid unneeded work, and as a way to control flow. Of the two, the second is much more meaningful. Here is a common idiom that uses it:

if (foo != null && foo->isValid()) {
    foo->doStuff();
}

foo->isValid() would dereference a null and cause undefined behavior if foo is null. Here, short circuit evaluation prevents that by simply not calling foo->isValid(). It can do that because "false and anything" will always stay false. That c++ actually does this is called short circuit evaluation. It also works with "true or anything". Just remember it evaluates from left to right. So you use the left to control the execution of the right.

  • You should be using foo->isValid() rather than foo.isValid(). – David Hammen Jul 16 '16 at 16:58
  • i think by null pointer exception you really mean undefined behavior / segfault. null pointer exception is java – Chris Beck Jul 16 '16 at 19:53
  • @ChrisBeck Thank you kindly as well. I've been staring at Java for too long. – candied_orange Jul 16 '16 at 20:02
1

The short circuit evaluation only works in cases where you can guarantee after evaluating the first operand that the end result won't be influenced by the second operand.

So for &&, if the first operand evaluates to true, you can't logically guarantee the result and have to evaluate the second operand (remember: && only returns true if both operands are true!). However, if the first operand is false, we can skip the evaluation of the second operand, since it won't change the result (false).

Nearly the same applies for ||, although you short circuit on a first operand that evaluates to true (since || returns true if either operand is true).

This might cause problems (or can be abused) when one or both operands are function calls, e.g. doStuff() || reportFailure(). reportFailure() will only be called if doStuff() returned false. This can sometimes cause confusion, especially if the code afterwards relies on some side-effect of reportFailure().

  • @The mysterious downvoter: would you please explain what can be done to improve on this answer? – hoffmale Jul 16 '16 at 20:09
1

The text you quoted would convince me never to let anyone buy this book.

&& and || are not bitwise operators, they are logical operators. & and | are bitwise operators, and they are not short-circuited.

Short-circuit operations are obviously different for && and ||. If you evaluate condition1 && condition2, then if condition1 is false, you know that condition1 && condition2 is false, so you don't evaluate condition2. If condition1 is true, then you don't know the result of the && yet; you need to evaluate condition2.

For ||, it is the exact opposite. If condition1 is true, then condition1 || condition2 is known to be true, so condition2 is not evaluated. If condition1 is not true, then we need to evaluate condition2.

1

The text you quoted is a bit sloppy (if for no other reason than && and || are not bitwise operators).

In the expression a && b, the expression b will be evaluated if and only if the expression a evaluates to true. If a evaluates to false, then a && b will evaluate to false regardless of the value of b, so b isn't evaluated at all:

a      b      result   b is evaluated
-      -      ------   --------------
true   true   true     yes
true   false  false    yes
false  ??     false    no

In the expression a || b, b will be evaluated if and only if a evaluates to false. If a is true, then a || b will evaluate to true regardless of the value of b, so b isn't evaluated at all:

a      b      result   b is evaluated
-      -      ------   --------------
true   ??     true     no
false  true   true     yes
false  false  false    yes

Unlike most operators in C, && and || force left-to-right evaluation and they introduce sequence points (a is evaluated and all side effects are applied before b is evaluated), so expressions like x++ && x++ are well-defined.

0

When the compiler sees the if statement:

if(condition1 && condition2)

It says to itself, condition 1 is true, so now my users wants to know if condition2 is also true.....hmmmm..... condition2 is false, therefore this statement is false

If you want to see a short circuit then refactor your if state to be:

if(condition2 && condition1)

Now the compiler will reason it out something like this:

"Ok, I have an if statement. The first argument is false. Now user wants me to 'and' it with something. But I don't even have to look at condtion1 since I'm sure this if statement evaluates to false.

So the compiler doesn't even bother to evaluate condtion1.

-1
if (condition1 && condition2)

Is same as

if (condition1 == true && condition2 == true)

Since your code shows :

bool condition1 = true;
bool condition2 = false;

If statement will not be true,because condition2 is false

Not the answer you're looking for? Browse other questions tagged or ask your own question.