Expressions connected by && or || are evaluated from left to right. i.e. if(a==1 && b==1 || c==1){} will return true without checking if c==1 or no.
my question here is if a==1 && b==1 && c==1 and evaluation is done from left to right is c being checked or no ?
don't have Arduino right now to check and couldn't find this type of question in the forum.
and this is the part of the code I`m struggling with:
if ((a >= 0 && a <= 13) && (b >= 33 && b <= 53) && (c >= 0 && c <= 30) && (d == 0 || d == -1000)) { do something}
will if statement check all condition inside and only after that execute expression?
or it will do only the first part checking between a && b without checking whatever is after 53) ?
If the a term is false, there is no point in checking further.
If the a term is true, then if the b term is false, there is no point in checking further.
If the a term is true and the b term is true, but the c term is false, there is no point in checking further.
If the a term is true and the b term is true and the c term is true, then (and only then) the results depend upon checking the d term.
However, I do not know whether this sort of short circuit evaluation is mandatory in C/C++.
I just want to be sure that all the terms listed will be checked if they are true. I don't care if one of them is false it can jump to do something else in the code .
but this is what I need:
if a term is true then don't skip but check also term b
if b is true don't skip continue checking
etc..
Rather than worrying about what the language does (which is well-defined and easily accessed by simply Googling "c++ expression evaluation"), why not simple use parentheses, to make your intent perfectly clear, and to remove any question?
RayLivingston:
Rather than worrying about what the language does (which is well-defined and easily accessed by simply Googling "c++ expression evaluation"), why not simple use parentheses, to make your intent perfectly clear, and to remove any question?
Regards,
Ray L.
Actually I thought I`m writing in C. couldn't get about parentheses can you write an example plz ?
thanks.
question of my first example I understood thanks everyone. this one is the part of code that I am not sure will check all conditions if all blocks from left to the right are true?
if ((a >= 0 && a <= 13) && (b >= 33 && b <= 53) && (c >= 0 && c <= 30) && (d == 0 || d == -1000))
can anyone check if this statement will work ?
don't have Arduino by hand right now.
a>=0 && a <=13 if this condition will be true will it continue checking or it will skip everything after it ?
a>=0 && a <=13 if this condition will be true will it continue checking or it will skip everything after it ?
Then it will check the next one in line (b >= 33 && b <= 53)
Question
Why would you write code if you can't test it? Wait till you have an Arduino at hand. Or use a normal C/C++ compiler (if you're using linux or mac; for windows you need to install one) and test this type of things.
vaj4088:
However, I do not know whether this sort of short circuit evaluation is mandatory in C/C++.
It is: it's a formal part of the language. In particular, a && b() will not invoke function b() if a is false. This is very important for things like possibly null pointers
Furthermore, there can't be any sensible reason to continue to check conditions that are provably "don't care" by the definitions of binary logic. What is your nonsensical reason?
I need all statements to be checked from left to right if one statement at the left is true continue checking if false from that part "I dont care" if it will continue checking or no cos whole if statement will be false then.
If a==b && c==d && e==f
Will do only first comparison regardless e==f or no.
Thats what I was worried about thats why I put all statements in brackets and was worried if that will work or no cos of that moment not having arduino to test on. If I will have arduino by hand will test it on it and will not post the question in the forum.
surepic:
I need all statements to be checked from left to right if one statement at the left is true continue checking if false from that part "I dont care" if it will continue checking or no cos whole if statement will be false then.
If a==b && c==d && e==f
Will do only first comparison regardless e==f or no.
Thats what I was worried about thats why I put all statements in brackets and was worried if that will work or no cos of that moment not having arduino to test on. If I will have arduino by hand will test it on it and will not post the question in the forum.
Thanks.
Perhaps you are confused about "operator precedence". The comparisons are always computed before the comparisons so brackets are unnecessary.
roosterqmoney:
it seems to me like you may need separate if statements.
Why? If statement is working I checked today on the board its working good.
All && are checked evaluations are also checked correctly.
There are more than 300 if statements and cases in the code. I was worried about too much ifs and switches but as I can see board is capable of handling them all very fast thanks @aarg for directing me to integer math instead of floating point.