PxPlus User Forum

Main Board => Discussions => Language => Topic started by: Jeffrey Ferreira on July 07, 2025, 01:41:36 PM

Title: Precedence / Logical Operators
Post by: Jeffrey Ferreira on July 07, 2025, 01:41:36 PM
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")
Title: Re: Precedence / Logical Operators
Post by: James Zukowski on July 07, 2025, 02:23:31 PM
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.
Title: Re: Precedence / Logical Operators
Post by: Jeffrey Ferreira on July 07, 2025, 03:02:15 PM
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?
Title: Re: Precedence / Logical Operators
Post by: James Zukowski on July 07, 2025, 03:04:47 PM
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...).
Title: Re: Precedence / Logical Operators
Post by: Devon Austen on July 09, 2025, 08:51:06 AM
For documentation on what the PxPlus order of precedence is see here:

https://manual.pvxplus.com/?PxPlus%20User%20Guide/Language%20Elements/Data%20Types,%20Literals%20and%20Variables/Numeric%20Values.htm#numeric_exp