Precedence / Logical Operators

Started by Jeffrey Ferreira, July 07, 2025, 01:41:36 PM

Previous topic - Next topic

Jeffrey Ferreira

Hi All,

When looking at code that didn't use parentheses I want to understand what pxplus is doing behind the scenes.

in the following

X=1 OR 0 AND 0
PRINT X

X WILL EQUAL 0.

Can someone state why this is? (Michael Scott: "Explain it me like I'm 5")

James Zukowski

From the online manual (Introduction to Using PxPlus -> Language Elements -> Data Type, Literals and Variables -> Numeric Values):

g - Logical Operators
When PxPlus encounters either an AND or an OR logical operator, it attempts to perform a shortcut in the evaluation of the expression. If the value to the left of an AND operator is zero (false), then the expression/value on the right is skipped and the relationship returns 0 (zero). If the value to the left of an OR is non-zero, then the expression/value on the right is skipped and the relationship returns 1.

In this case, there are two expressions being evaluated: "1 OR 0" and "<result> AND 0"
The result of "1 OR 0" is 1, which is fed to the second expression of "1 AND 0", yielding 0.
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

Jeffrey Ferreira

Hi James,
Thank you. I did read that. I dont know why but i had it my head that AND's would get executed first and i'm of course wrong.

Stated another way -> would you agree that in the absence of parentheses that multiple and/or's would get executed from left to right?

James Zukowski

That is "standard procedure" for any expressions. Those expressions at the same "precedence level" will be operated on from left to right.
And yes, parentheses will clarify any precedence questions (though not necessarily make it easier to read...).
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

Devon Austen

Principal Software Engineer for PVX Plus Technologies LTD.