# short circuit boolean evaluation

**URL:** <https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750>\
**Category:** Programming\
**Created:** [January 6, 2013, 4:58am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750 "2013-01-06T04:58:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![freakdaddy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/freakdaddy/32/1013044_2.png) [@freakdaddy](https://forum.arduino.cc/u/freakdaddy)\
**Post date:** [January 6, 2013, 4:58am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/1 "2013-01-06T04:58:58Z")

</div>

Does the arduino language support short circuit boolean evaluations such that digitalRead in the below example will never run because the first part of the logic statement is false?

```cpp
if false && digitalRead {
//...
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 5:01am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/2 "2013-01-06T05:01:21Z")

</div>

you should actually pose all your program... n no you cant just use false like that... it must be somthing that it false

---

<div class="post-metadata">

**Author:** ![freakdaddy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/freakdaddy/32/1013044_2.png) [@freakdaddy](https://forum.arduino.cc/u/freakdaddy)\
**Post date:** [January 6, 2013, 5:02am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/3 "2013-01-06T05:02:40Z")

</div>

I'm just asking about a concept. I have no code to post at this point. Consider it psuedo-code.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 5:18am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/4 "2013-01-06T05:18:11Z")

</div>

for every input you need an adreess that is in a boolean state lets say you have a LED State  
you could write it as  
if(LEDstate==false&&something==true) semthing heppen

or you id you need something to be control by the state of a pin  
you could  
int INpin=digitalRead(4);  
if (INpin==true&&something==true)something happen;

or lets say you dont want to have to declare a new veriable u could also  
if (digitalRead(4)==true&&something==true)something happen;

HIGH is the same as 1 and true  
LOW is the same as 0 or false

if false && digitalRead {  
//...  
}

this cant be use cause there is nothing to compare it with. i dont know if its can compile or not but is there any idea what is false in that context?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 5:24am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/5 "2013-01-06T05:24:34Z")

</div>

ouh yeah if you have declare a veriable to be boolean then you could use this

if(LEDstate&&MotorState){do something;}

what it means is that if ledstate is true/high/1 and motor state is true/high/1 then do something happen

if (!LEDstate&&!motorstate){do something ;}  
what it means is if not led state (which inverse everything rite) or you could say that if led state is false/LOW/0 and motor state is 0/false/LOW then do something happen

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [January 6, 2013, 5:29am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/6 "2013-01-06T05:29:59Z")

</div>

> freakdaddy:  
> Does the arduino language support short circuit boolean evaluations such that digitalRead in the below example will never run because the first part of the logic statement is false?

Yes. The Arduino IDE runs the GCC C++ compiler so short-circuit evaluation is enabled.

---

<div class="post-metadata">

**Author:** ![freakdaddy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/freakdaddy/32/1013044_2.png) [@freakdaddy](https://forum.arduino.cc/u/freakdaddy)\
**Post date:** [January 6, 2013, 5:31am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/7 "2013-01-06T05:31:22Z")

</div>

That's what I was looking for! Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 5:41am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/8 "2013-01-06T05:41:13Z")

</div>

ouh well then my reply on reply #4 is true just that i dont know what it means, haha btw Coding badly im wondering what does this mean

```cpp
if(false&&true);

```

it compile but i want to know is there any application for it?

---

<div class="post-metadata">

**Author:** ![freakdaddy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/freakdaddy/32/1013044_2.png) [@freakdaddy](https://forum.arduino.cc/u/freakdaddy)\
**Post date:** [January 6, 2013, 5:43am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/9 "2013-01-06T05:43:46Z")

</div>

It wasn't about using false and true in the evaluation. It was about short-circuit evaluation. My code was just an example. Nothing you would use is real life. Read this...

> **[Short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation)**
>
> Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: 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.
> I...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 5:47am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/10 "2013-01-06T05:47:58Z")

</div>

hehe i read it after Coding Badly said its ok.... and yeah sorry about that freakdaddy. I kinda miss leading you for a while there. I dont know what Short circuit evaluation b4 now i know better.

---

<div class="post-metadata">

**Author:** ![freakdaddy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/freakdaddy/32/1013044_2.png) [@freakdaddy](https://forum.arduino.cc/u/freakdaddy)\
**Post date:** [January 6, 2013, 5:53am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/11 "2013-01-06T05:53:18Z")

</div>

No problem. Appreciate you trying to help. I pay attention to short circuit evaluation when I'm trying to loop quickly. Saves some clock cycles by skipping some digitalReads here and there.

---

<div class="post-metadata">

**Author:** ![JimboZA](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimboza/32/215171_2.png) [@JimboZA](https://forum.arduino.cc/u/JimboZA)\
**Post date:** [January 6, 2013, 5:57am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/12 "2013-01-06T05:57:47Z")

</div>

> my reply on reply #4

> if(LEDstate&&MotorState){do something;}

Yeah Ash what the short circuit thing seems to mean is that, in your example #4, if LEDstate is false, there's no point it evaluating MotorState because even if MotorState is true, the && comes out false and it knows that as soon as it sees LEDstate is false.

So my supplementary question to this: why would we ever _not_ short-circuit? I'm clearly missing something here....

---

<div class="post-metadata">

**Author:** ![freakdaddy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/freakdaddy/32/1013044_2.png) [@freakdaddy](https://forum.arduino.cc/u/freakdaddy)\
**Post date:** [January 6, 2013, 6:03am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/13 "2013-01-06T06:03:12Z")

</div>

I think most often short circuit is ok but I did not know if the arduino compiler supported it or not. There may some situations where you want to turn it off but my guess is that there are other ways to fix your code so you don't have to.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 6:08am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/14 "2013-01-06T06:08:14Z")

</div>

in that case why not make every thing that is more likely to be dominant to be at the front and make every argument as an and? is it a more likely scenario? or  
we could so a couple of nested if?  
like  
if(ledState)  
{  
if(motorstate)  
{  
}  
else  
{  
}  
}  
else  
{  
}

just saying my thought on this not necessarily mean true.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [January 6, 2013, 6:33am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/15 "2013-01-06T06:33:27Z")

</div>

> JimboZA:  
> So my supplementary question to this: why would we ever _not_ short-circuit? I'm clearly missing something here....

Technically, a procedural language like C and C++ should never have short-circuit evaluation. The problem is that functions are allowed to have side-effects (in plain English, functions are allowed to muck with global data). Imagine we have two functions (_CheckFan_ and _CheckHeater_) that return a boolean value and save some status information for the next call...

if ( CheckFan() && CheckHeater() )  
{  
// do something  
}

If _CheckFan_ returns false, _CheckHeater_ is not called so there is no opportunity to update the status. In some cases, this can result in undesirable behaviour (bugs).

However, good programming practices dictate that a function that has side-effects should not be used in a condition # which eliminates the problem. In other words, we can have our cake and eat it too if we just follow a few basic rules.

Typically, short-circuit evaluation is turned off so legacy (i.e. poorly written) code works correctly.

# Continuing with the example, we might break our two functions into four: _UpdateFanStatus_, _UpdateHeaterStatus_, _CheckFan_, _CheckHeater_...

UpdateFanStatus();  
UpdateHeaterStatus();

if ( CheckFan() && CheckHeater() )  
{  
// do something  
}

---

<div class="post-metadata">

**Author:** ![JimboZA](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimboza/32/215171_2.png) [@JimboZA](https://forum.arduino.cc/u/JimboZA)\
**Post date:** [January 6, 2013, 6:52am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/16 "2013-01-06T06:52:14Z")

</div>

Thanks Coding Badly.... karma++ 8)

I'm new to C, and I don't think I had realised a _function_ could be in an if... I guess I thought it was for _variables_ only. I probably got that impression from the way it's explained [here](http://arduino.cc/en/Reference/If).

Your explanation that if a function is in an if, then it gets called and re-evaluated there and then makes sense.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [January 6, 2013, 7:00am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/17 "2013-01-06T07:00:00Z")

</div>

You are welcome.

Yeah, that page is very "variable centric". I can certainly understand why you got that impression. The odd thing is that if ( digitalRead( BUTTONPIN ) ) has to be, by far, the most common Arduino condition but it isn't mentioned on that page.

Just to be quite clear: Using a function in a condition is not evil. Using a function that has side-effects in a condition can be evil (try to avoid it).

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 11:40am UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/18 "2013-01-06T11:40:22Z")

</div>

> ash901226:  
> you should actually pose all your program... n no you cant just use false like that... it must be somthing that it false

Rubbish.

You can use literal true and false if you want. However, the expression would need to be enclosed in parenthesis to compile:

```arduino
if (false && digitalRead) {
//never executes because false is never true
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 6, 2013, 12:16pm UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/19 "2013-01-06T12:16:00Z")

</div>

peterH haha, if you read from the very beginning to the end you just might see that during all the long reply i had change my mind....

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [January 6, 2013, 2:30pm UTC](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750/20 "2013-01-06T14:30:06Z")

</div>

@ash901226

The best known purpose of using the literals true and false are the endless loops and forced else branches

```cpp
while (true) { ... }  // endless loop

// used in debugging sessions to force the route through the code 
// suppose you need to debug the branches of this if statement individually 
if (some_condition) { ... } else { ... }  

//to force the use of the if branch add: 
if ((some_condition) || true) { ... } else { ... }  

// to force the use of the else branch add:
if ((some_condition) && false) { ... } else { ... }

```

This debugging trick is less known but is sometimes useful.  
It is fast and much easier to apply than #ifdef 's or patching the variables so that the conditions returns false.

[Next page](https://forum.arduino.cc/t/short-circuit-boolean-evaluation/137750.md?page=2)
