Multiple && checking

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

The answer is: it depends.

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?

Regards,
Ray L.

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.

Just and extra set of ()

if (a==1 && (b==1 || c==1))
{
}

if ((a==1 && b==1) || c==1)
{
}

It depends on your needs

The first one will

evaluate a==1;
if the result is false, the rest of the checks will be skipped
if the result is true, it will evaluate the second part between the ()

evaluate b==1
if the result is, the check for c will be skipped
if the result is false, the c==1 will be evaluated

appreciate your time for trying to help me.

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.

Thanks everybody.

I was just modifying my code wasn't sure if it will work properly on a device or not.

About compiler never even thought about that have to download I guess.

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

if(a && ++*a < 10) {
}

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.

Thanks.

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.

@aarg

You are actually right im confused with"precedence" have to read that part in the book I guess to clearly understand.

PaulMurrayCbr:

if(a && ++*aa < 10) {

}

Is that missing an "a"?

surepic:
@anon57585045

You are actually right im confused with"precedence" have to read that part in the book I guess to clearly understand.

It's like high school algebra where 1+2*3 equals 7, not 6.

it seems to me like you may need separate if statements.

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.

aarg:
It's like high school algebra where 1+2*3 equals 7, not 6.

Like you I did not do too well in high school algebra. :slight_smile:

justone:
Like you I did not do too well in high school algebra. :slight_smile:

That was just a typo. I meant to type 9. :slight_smile: