# Correct construct of if satement

Could someone verify of i constructed this if correctly:

if(xStop1 == HIGH || xStop2 == HIGH && hitXStop == false && state == TRACKING) {
This should be true if either xStop is High
and hitXStop is false and state euals tracking.
}

The reason for asking is that it works in all states.
Anny suggestions?

Harry

Parentheses would make your intended logic more evident to the reader and the compiler.

You REALLY should use parentheses to make the order absolutely clear, to yourself and others. Evaluating the terms left-to-right, and right-to-left will give different results, and the reader should not have to remember, or look up, the C parsing order to understand the code.

Did you mean:

if( ( (xStop1 == HIGH) || (xStop2 == HIGH) ) && (hitXStop == false) && (state == TRACKING) )

or:

if( (xStop1 == HIGH) || ( (xStop2 == HIGH) && (hitXStop == false) && (state == TRACKING) )

Regards,
Ray L.

&& = AND operator will rule like x * y.
Then the || = OR operator will be considered like x *y - a * b.
For your eyes only, it will make it easier to read using parentheses :

if((xStop1 == HIGH) || (xStop2 == HIGH && hitXStop == false && state == TRACKING)) {
This should be true if either xStop is High
and hitXStop is false and state equals tracking.
}

In your case the if logic translates to :
if either
xStop1 is true
or
(xStop2 is true AND hitXStop is false AND state is tracking) is true at the same instant
then you execute the next following statements.

That is not what you want according to your comment.
You want to do :

if(xStop1 == HIGH || xStop2 == HIGH)
{
if(hitXStop == false && state == TRACKING)
{ // do your stuff here
} // end inner if
} // end outer if

Assuming that the state does not change very much, you might want to put it first so it will "short circuit" and not even look at the other variables if state != tracking.

It is extraordinarily easy to make mistakes with IF statements with multiple tests - particularly if there is an ELSE part.

I prefer to set them out as a cascade of separate tests so I can be more certain what I am doing. I doubt if it makes any difference to the compiler.

You may also find that one of the tests is more important, or that it embraces some other tests so that you don’t need to repeat it in a succession of complex IF statements.

…R

Or the state variable leads me to believe that a switch statement implementation of a state machine might be the way to go.

It's not a good idea to test if things are "equal to false".

michinyon:
It's not a good idea to test if things are "equal to false".

It is still correct syntax, if the variable is a boolean type.

michinyon:
It's not a good idea to test if things are "equal to false".

Not sure why you would think that.... It is every bit as valid as testing "equal to true". true and false are both numeric constants, and both equally valid.

Regards,
Ray L.

gharryh:
Could someone verify of i constructed this if correctly:

if(xStop1 == HIGH || xStop2 == HIGH && hitXStop == false && state == TRACKING) {
This should be true if either xStop is High
and hitXStop is false and state euals tracking.
}

The reason for asking is that it works in all states.
Anny suggestions?

Harry

Can you clarify this it works in all states.?

The condition / statement is syntactically correct and follows your requirement.
There is no need to add any other formatting if one knows how your condition is evaluated.
Perhaps if you look at the logic from a different angle. Instead of "should be true if either xStop is High" say "will be false if both xStopx are false" may put a different spin on the nonexistent "problem".