Using compound operators in an IF statement


I have an if statement along the lines of if(AA==BB && CC==DD && FF==GG OR FF might==HH) In other words, one of the ANDs can return true from a choice of values (GG or HH).

I wrote it as

if(startByte==0x7E && lengthByteLSB>=12 &&calculatedID==0x40B31540|| calculatedID==0x40F90403 ||calculatedID==0x40C0E5F2 ||calculatedID==0x40B41623 ||calculatedID==0x40F90353)

For information, the IDs are XBee addresses, so if the startByte and lengthByteLSB are correct and the calculatedID is one of the five addresses, the if statement executes.

Or put it another way, is there an order of precedence for operators or should I be using brackets?

It seems to work OK

Thanks for any advice

There is an order of precedence but brackets (parentheses) will make it very clear what your intentions are, even if your intentions are identical to the order of precedence.

Thank you both The precedence table looks very useful

tigger: Thank you both The precedence table looks very useful

I would suggest using brackets to make the intentions of the code more obvious.

Brackets are not braces are not parentheses. If you don't know these terms, google it. Then all your talking about syntax will be a bit more on track with the real world.

In mathematics they all carry the same meaning, that is to enforce order of computation. Not so much in computer languages.

[{[( ! )]}]

And I agree: The precedence table is very useful, but sometimes it makes good sense to add a little extra ink to make sure you get what you want and those of us not intimately familiar with C or C++ or whatever language you are using get what you mean. And of course so that the compikler also gets it "right".


tigger: The precedence table looks very useful

Yes, It's useful but challenging.

"Make your code readable. Pretend the next person who looks at your code is a psychopath and they know where you live." - Philip Wadler