Focal Point
[SOLVED] Logical operator precedence

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/2847074806

August 17, 2011, 04:00 AM
Wep5622
[SOLVED] Logical operator precedence
I'm looking at an existing piece of code here (not mine) trying to figure out how it evaluates...

It contains something along these lines:
DEFINE FILE x
A/I1 = IF expr1 AND expr2 OR expr3 THEN 1 ELSE 0;
END

Does that logical expression evaluate as:

Can you also explain why it evaluates to that one instead of the other?


In the Help I found this list of logical expressions: Creating Reports With Report Painter > Writing Expressions > Writing Logical Expressions

But it doesn't say in what order they get evaluated or what their precedence is.

This message has been edited. Last edited by: Wep5622,


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
August 17, 2011, 06:23 AM
Swap
I think if you do not provide any distribution of conditions through brackets, WF treates the expression as option 2.


Webfocus 7.7.03
Windows XP
Excel, PDF, HTML, APDF, AHTML, Maintain
August 17, 2011, 07:58 AM
Wep5622
Okay, thanks. That would mean that OR has precedence over AND.


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
August 17, 2011, 10:52 AM
Dave
Assuming WF uses the same precendence as the other languages, the order would be:
(),NOT,AND,OR


_____________________
WF: 8.0.0.9 > going 8.2.0.5
August 17, 2011, 10:59 AM
GamP
Of course, you could also try to create a small procedure to check the precedence, like:
DEFINE FILE CAR
A/A1 = IF COUNTRY EQ 'ENGLAND' AND CAR EQ 'TRIUMPH' OR BODYTYPE EQ 'COUPE' THEN 'A' ELSE ' ';
B/A1 = IF (COUNTRY EQ 'ENGLAND' AND CAR EQ 'TRIUMPH') OR BODYTYPE EQ 'COUPE' THEN 'B' ELSE ' ';
C/A1 = IF COUNTRY EQ 'ENGLAND' AND (CAR EQ 'TRIUMPH' OR BODYTYPE EQ 'COUPE') THEN 'C' ELSE ' ';
END
TABLE FILE CAR
PRINT COUNTRY CAR BODYTYPE A B C
END
and then see what happens...


GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
August 17, 2011, 11:37 AM
Wep5622
I couldn't come up with a simple reliable test-case, my brain is currently too numb with a lot of DIY-work at home after my day-job...

An extra benefit is of course that this is now documented in Google.

The results are:
  COUNTRY     CAR               BODYTYPE      A  B  C                                                                             
  -------     ---               --------      -  -  -                                                                             
  ENGLAND     JAGUAR            CONVERTIBLE          
  ENGLAND     JAGUAR            SEDAN                
  ENGLAND     JENSEN            SEDAN                
  ENGLAND     TRIUMPH           HARDTOP       A  B  C
  JAPAN       DATSUN            SEDAN                
  JAPAN       TOYOTA            SEDAN                
  ITALY       ALFA ROMEO        SEDAN                
  ITALY       ALFA ROMEO        COUPE         A  B   
  ITALY       ALFA ROMEO        ROADSTER             
  ITALY       MASERATI          COUPE         A  B   
  W GERMANY   AUDI              SEDAN                
  W GERMANY   BMW               SEDAN                
  W GERMANY   BMW               SEDAN                
  W GERMANY   BMW               SEDAN                
  W GERMANY   BMW               SEDAN                
  W GERMANY   BMW               SEDAN                
  W GERMANY   BMW               SEDAN                
  FRANCE      PEUGEOT           SEDAN                


From which the conclusion can be drawn that AND takes precedence over OR, as implementations 'A' and 'B' behave the same.


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
August 17, 2011, 01:41 PM
j.gross
quote:
Assuming WF uses the same precendence as the other languages, ...


The headwaters of FOCUS expression semantics lie in FORTRAN.
(And Dialog Manager syntax is rooted in VM's EXEC I, but that's another discussion).


Just to eliminate the possibility that FOCUS gives AND and OR equal precedence, combining the terms in lexical order (a la APL, my eldest son's first computer language):

DEFINE FILE CAR
A/A1 = IF (COUNTRY EQ 'ENGLAND') AND (CAR EQ 'TRIUMPH') OR (BODYTYPE EQ 'COUPE') THEN 'A' ELSE ' ';
a/A1 = IF (BODYTYPE EQ 'COUPE') OR (CAR EQ 'TRIUMPH') AND (COUNTRY EQ 'ENGLAND') THEN 'a' ELSE ' ';
B/A1 = IF (COUNTRY EQ 'ENGLAND' AND CAR EQ 'TRIUMPH') OR BODYTYPE EQ 'COUPE' THEN 'B' ELSE ' ';
C/A1 = IF COUNTRY EQ 'ENGLAND' AND (CAR EQ 'TRIUMPH' OR BODYTYPE EQ 'COUPE') THEN 'C' ELSE ' ';
END
TABLE FILE CAR
PRINT COUNTRY CAR BODYTYPE A a B C
END

a is seen as equivalent to A, so the equivalence to B is (indeed) based on precedence rather than lexical order


- Jack Gross
WF through 8.1.05
August 18, 2011, 04:04 AM
Wep5622
I knew there was more to it! Thanks for confirming that.


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
August 22, 2011, 01:13 PM
ABT
Obligitory posting that it is always best practice to use brackets to group items so they are are forced to eval as intended versus relying on the interpreter to do the needful.

Or, as your coworker: Make this easy so I don't have to think about it.

- ABT


------------------------------------
WF Environment:
------------------------------------
Server/Client, ReportCaster, Dev Studio: 7.6.11
Resource Analyzer, Resource Governor, Library, Maintain, InfoAssist
OS: Windows Server 2003
Application/Web Server: Tomcat 5.5.25
Java: JDK 1.6.0_03
Authentication: LDAP, MRREALM Driver
Output: PDF, EXL2K, HTM

------------------------------------
Databases:
------------------------------------
Oracle 10g
DB2 (AS/400)
MSSQL Server 2005
Access/FoxPro