Search code examples
ibm-midrangerpgle

Boolean order of operation in RPGLE


Why does the below code result in true if type = 7 and seq = 224?

RPGLE free form:

if (type = 6 or                 
    type = 7 or                 
    type = 9) and               
    not (seq = 224 or seq=249);

I had to rewrite it to:

if (type = 6 or                 
    type = 7 or                 
    type = 9) and               
    seq <> 224 and 
    seq <> 249;

but why? I am guessing it has something to do with the NOT operator.


Solution

  • Well, NOT has a higher precedence than AND or OR RPG IV Reference manual operator precedence

    1. ()
    2. Built-in functions, user-defined functions
    3. unary +, unary -, NOT
    4. **
    5. *, /
    6. binary +, binary -
    7. =, <>, >, >=, <, <=
    8. AND
    9. OR

    However, the expression you show should evaluate to false...

    **free
    
     dcl-s flag ind;
     dcl-s seq int(5) inz(224);
     dcl-s type int(5) inz(7);
    
       flag = (type = 6 or
               type = 7 or
               type = 9) and
                not (seq = 224 or seq=249);
    
       dsply ('Flag=' + flag);
       *INLR = *ON;
       return; 
    

    Joblog shows:

    DSPLY Flag=0