Or Not Equal To Syntax for RPG Programming

Anthony6890
Anthony6890 used Ask the Experts™
on
Hi Everyone,

I'd just like to confirm a line of code that I'm modifying in an RPG program...

This is what the code currently is:

 MBFTPT    IFEQ 'P'      
 MBSGNP    IFNE 'L'      
 MBSS#     CHAINLABGOOD  

Open in new window


I need to add another line for MBSGNP that if it is not equal 'O' as well.  But I'm not completely confident on the syntax of the "or not equal to part".   Can someone assist me with this.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,
I am not sure exactly what you need, but try a small test program to see how it works.

Here is some sample code for RPG program TEST:
C           *ENTRY    PLIST                          
C                     PARM           MBFTPT  1       
C                     PARM           MBSGNP  1       
C           MBFTPT    IFEQ 'P'                       
C           MBSGNP    IFNE 'L'                       
C           MBSGNP    IFNE 'O'                       
C           'CHAIN'   DSPLY                          
C                     END                            
C                     END                            
C                     END                            
C                     SETON                     LR   
C                     RETRN                          

Open in new window

and here are some test cases.
call test (P O)
call test (P L)
call test (Q O)
call test (Q L)
call test (X L)
call test (X X)
call test (P X)
DSPLY  CHAIN    

Hope this helps,
Barry

Author

Commented:
Thanks Barry. I was originally going your route as well but then I came across this code on the IBM site...

ORNE 'O'

and when I went to compile it compiled correctly. we are running the program tomorrow night so I'll let you know if I had to change it to match your logic...
Here is a program using the ORNE instead of IFNE.
Review the test cases below.
Note that (MBSGNP not equal 'L'  OR MBSGNP not equal 'O') will always be true regardless of the value of MBSGNP  because it can only have one value so it will always be not equal to the other.

C           *ENTRY    PLIST                        
C                     PARM           MBFTPT  1     
C                     PARM           MBSGNP  1     
C           MBFTPT    IFEQ 'P'                     
C           MBSGNP    IFNE 'L'                     
C           MBSGNP    ORNE 'O'                     
C           'CHAIN'   DSPLY                        
C                     END                          
C                     END                          
C                     SETON                     LR 
C                     RETRN                        

Open in new window


call test (P O )
DSPLY  CHAIN    
call test (P L )
DSPLY  CHAIN    
call test (Q O )
call test (Q L )
call test (X L )
call test (X X )
call test (P X )
DSPLY  CHAIN
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

VP Technology / Senior Consultant
Commented:
This is a "Programming 101" issue, not an RPG syntax issue.  Remember how a logical OR works.  Yes, we all understand what you mean when you say verbally:

"Do this if MBSGNP is not 'L' or 'O'".

What you really mean "the value of MBSGNP is not a member of the set {'L','O'}", but people who aren't math professors don't talk like that.

But that's not a logical OR operation.  In a logical OR, both sides of the OR are evaluated first, and then the OR operation is performed between the two:

Logical OR truth table

TRUE OR TRUE = TRUE
FALSE OR TRUE = TRUE
TRUE OR FALSE = TRUE
FALSE OR FALSE = FALSE

So:

Case: MBSGNP = 'L'

MBSGNP <> 'L' = FALSE (because this is a NOT EQUAL test.  If they are not equal, result is TRUE.  If they ARE equal, it is FALSE)
MBSGNP <> 'O' = TRUE
False OR True =  TRUE, because if either side of the OR evaluates to TRUE, the expression is TRUE

Case: MBSGNP = 'L'
MBSGNP    IFNE 'L'                   Evaluates to TRUE, not equal to 'L'
MBSGNP    ORNE 'O'                Evaluates to FALSE, -NOT- not equal to 'O'
MBSS#     CHAINLABGOOD    So, CHAIN GETS EXECUTED

Case: MBSGNP = 'O'
MBSGNP    IFNE 'L'                   Evaluates to FALSE
MBSGNP    ORNE 'O'                Evaluates to TRUE
MBSS#     CHAINLABGOOD    CHAIN GETS EXECUTED

Case: MBSGNP = 'X'
MBSGNP    IFNE 'L'                   Evaluates to TRUE (X is not L)
MBSGNP    ORNE 'O'                Evaluates to TRUE (X is not O)
MBSS#     CHAINLABGOOD    CHAIN GETS EXECUTED

Only way you can get the chain to execute is if BOTH clauses evaluate to FALSE.  That's impossible, since you are testing the same variable for two different values.

I suspect that you only want to CHAIN when both conditions are true.  

MBSGNP is not' L'
AND
MGSGNP is not 'O'.  

So:

Case: MBSGNP = 'X'
MBSGNP    IFNE 'L'                   Evaluates to TRUE
MBSGNP    ANDNE 'O'             Evaluates to TRUE
MBSS#     CHAINLABGOOD    CHAIN is not executed

Author

Commented:
Hi Gary, that makes complete sense and I don't know why I didn't catch it.  I just made the adjustment to my program.  We'll be running int tomorrow so I'll let you know if it worked correctly.

Thanks for the assistance.
Gary PattersonVP Technology / Senior Consultant

Commented:
RPGIII syntax is awkward at best, and without being able to express complex logic as expressions, as in other modern languages, it makes things like this more complicated.  

Plus, I see "AND/OR" confusion when debugging code in many languages - including my own code.  Sometimes it just takes another pair of (less tired?) eyes.

By the way, in Free Form ILE RPG IV, the logic would look like this:

if MBFTPT = 'PT';
   if ((MBSGNP <> 'L') and (MBSGNP <> 'O'));
      chain MBSS# LABGOOD;  
   endif;
endif;
Theo KouwenhovenApplication Consultant

Commented:
/Free
   Select;
      When MBFTPT = 'P'                       
         If %scan(MBSGNP:'LOX') = 0;                          
            DSPLY  'CHAIN'                            
         EndIf;
      When MBFTPT = 'Q'                       
         If %scan(MBSGNP:'LO') = 0;                          
            DSPLY  'CHAIN'                            
         EndIf;
      When MBFTPT = 'X'                       
         If %scan(MBSGNP:'LX') = 0;                          
            DSPLY  'CHAIN'                            
         EndIf;
   EndSl;
/End-Free

Open in new window

Author

Commented:
Gary, your logic worked flawlessly.  Thanks again for the assistance and the education.
Gary PattersonVP Technology / Senior Consultant

Commented:
@Theo,

%SCAN function looks for the entire string, not individual characters of the string, so that code isn't going to do what Anthony wants.  I think you mean %CHECK.
Theo KouwenhovenApplication Consultant

Commented:
Gary,




I know what the %scan is doing :-)

 %scan(MBSGNP:'LOX')  returns 1,2 or 3 and 0 if it is a not Equal situation
I prefer this because I can test several NE values at the same time

If %scan(A:'YN') + %scan(B:'XYZ') + %scan(c:'123') = 0;   // = nor

If %scan(A:'YN') + %scan(B:'XYZ') + %scan(c:'123') > 0;   //= or


BTW in the question there are 2 variables MBFTPT and  MBSGNP
Gary PattersonVP Technology / Senior Consultant

Commented:
Hi Theo,

Sorry, misread the direction of the scan.  I get it now.  Nice technique.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial