short circuit boolean evaluation

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?

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

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

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

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?

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

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.

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

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

if(false&&true);

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

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...

http://en.wikipedia.org/wiki/Short-circuit_evaluation

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.

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.

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....

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.

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.

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
}

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.

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

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).

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:

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

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....

@ash901226

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

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.