-5

Here is some code to check that a value falls in between a range

 if((i <= n) && ( (i+m) >= n){
   //do something.....
 };

This is basically a Boolean AND condition, and if both operands are true then the outcome of the condition is true. However with short circuit evaluation C++ only tests to see if the first condition is true, and if so, then it never checks whether the second condition is true. In the proper mathematical sense how is this a Boolean test? This totally deranges the logic of my code and instead I had to write it like this:

 if( (i <= n){
    if((i+m) >= n){
       //do something......
    }
 }

What is the sense of short-circuit evaluation, and how can I make C++ do a proper Boolean test without using nested if conditions.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    You cheat on your wife if it's Tuesday AND if she is visiting her parents. Do you cheat on her every Tuesday? – Kerrek SB Jul 21 '15 at 11:41
  • 2
    Someone needs to go review their [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) – Cory Kramer Jul 21 '15 at 11:41
  • You have a typo in the second condition - should be i+m – cup Jul 21 '15 at 11:41
  • You are missing a close parentheses in the top code sample. There are 4 opening and 3 closing parentheses. Anyhoo I can still follow along. – A.J. Jul 21 '15 at 12:49

5 Answers5

8

However with short circuit evaluation C++ only tests to see if the first condition is true, and if so, then it never checks whether the second condition is true.

That is the case for ||, but not &&. && short circuits if the first condition is false. Your two snippets are equivalent.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
2

Just to be sure that there is no misunderstanding here:

(a AND b)

will be FALSE for any value of b as long as a is FALSE. Similarly,

(a OR b)

will be true for any value of b as long as a is TRUE.

So, in C++, (a && b) is short-circuited when a is false, and (a || b) is short-circuited when a is true.

Conditions usually don't have side effects. If they do, you can be "clever" and do:

if (a && do_stuff()) {}

instead of

if (a) { do_stuff(); }

or be really silly and do

if (a && do_stuff()) { do_more_stuff(); }

when you actually meant to do:

if (do_stuff() && a) { do_more_stuff(); }

which is why you don't usually have conditions with side effects. They are of course useful and used. And since both the evaluation order and the short-circuiting are part of the standard, you can safely use them.

Community
  • 1
  • 1
2

The flaw here is in your understanding of the short-circuit rules, not with the rules themselves. For any valid expressions a and b

  if (a && b) ...

will always evaluate a. If a is true, then b will always be evaluated. The short-circuiting only occurs if a is false .... since that guarantees a && b is false, without the need to evaluate b.

Peter
  • 35,646
  • 4
  • 32
  • 74
1

I think you are a bit confused with the short-circuit operators :)

  • For &&, if the left-hand side expression is false, the combined result is false (the right-hand side expression is never evaluated).
  • For ||, if the left-hand side expression is true, the combined result is true (the right-hand side expression is never evaluated).

This is mostly important when the right-hand expression has side effects, such as altering values(or calling some functions with side-effects), for example:

if ( (itr < 10) && (++itr < n) ) {

//do stuff

}

Note that the combined conditional expression would increment itr by 1, only if the conditional expression on the left of && is true, because otherwise, the conditional expression on the right-hand side i.e. (++itr < n) is never evaluated.

Community
  • 1
  • 1
TryinHard
  • 4,078
  • 3
  • 28
  • 54
0

Yes of-course!

With a Boolean AND, if a is false, and b is true, then (a AND b) is false. Similarly, if a is false and b is false, then (a AND b) is false.

With an AND condition if C++ determines that the left hand operand is false then there is no need to check the second operand since the entire condition is false. Hence, the benefit of short-circuit evaluation.