Conditional expressions

Conditional expressions are used in combination with the IF and DO statements to manipulate and select data in the Job Activity section.

When an IF statement is present, the statements following the IF statement are processed based on the truth of the conditional expression. Statements are processed until an END-IF or an ELSE statement is encountered.

When a DO statement is present, all statements following the DO statement are processed, based on the truth of the conditional expression, until and END-DO statement is encountered.

The IF statement syntax

Read syntax diagramSkip visual syntax diagramIF&FIELD1EQ =NE (not)=GT >GE >=LT <LE <=&FIELD2LITERALArithmetic ExpressionStatements to be processed for true outcomeELSEStatements to be processed for false outcomeEND-IF

The DO statement syntax

Read syntax diagramSkip visual syntax diagramDOWHILEUNTIL&FIELD1EQ =NE (not)=GT >GE >=LT <LE <=&FIELD2LITERALArithmetic ExpressionStatements to be processed for true outcomeEND-DO
Parameters
&FIELD1
A field name used as argument 1 in comparison
&FIELD2
A field name used as argument 2 in comparison. The field must be of the same type as &FIELD1. So if &FIELD1 is numeric then &FIELD2 must be numeric.
LITERAL
A numeric or an alphanumeric literal, depending on the type of &FIELD1. Numeric literals can have a leading “+” or “-”. Multiple literals can be listed. Also, the THRU statement can be used to denote a range of low to high values.
Arithmetic Expression
Can be any arithmetic expression. Valid only when &FIELD1 is numeric.

The IF statement bit testing

Read syntax diagramSkip visual syntax diagramIF&FIELD1ON&FIELD2HEX LITERALStatements to be processed for true outcomeELSEStatements to be processed for false outcomeEND-IF

The IF statements can be nested. Migration Utility supports up to NESTS=NN of IF nests (refer to EXPARAMS NESTS=parameter). Any expressions that contain unreasonable levels of IF nests have to be split into multiple expressions to satisfy the limit.

Migration Utility does limited checking for compatible Fields Class of IF arguments. Any missed non-compatible arguments are flagged by the COBOL compiler.

Conditional expressions should be kept as simple as possible. More complex expressions are harder to understand and, sometimes, can lead to absurd outcomes.

Easytrieve allows comparison on a range of values via a THRU statement. The THRU range is translated by Migration Utility to a COBOL equivalent expression, depending on the last interpreted relational/logical operator.

For example, Easytrieve statement
   IF FIELDA EQ 10 THRU 55
is converted to COBOL as
   IF (FIELDA NOT > 55 AND NOT < 10)
whereas
    IF FIELDA NE 10 THRU 55
is converted to COBOL as
   IF (FIELDA < 10 AND > 55)

Easytrieve allows comparison on a list of values. The list is translated by Migration Utility to a COBOL equivalent expression, depending on the last interpreted relational/logical operator.

For example, Easytrieve statement
    IF FIELDA EQ 10, 15, 20, 25
is converted to COBOL as
    IF (FIELDA = 10 OR 15 OR 20 OR 25)
whereas
    IF FIELDA NE 10, 15, 20, 25
is converted to COBOL as
    IF (FIELDA NOT = 10 AND 15 AND 20 AND 25)

There are some differences in the way COBOL and Easytrieve Plus evaluate the IF statement. For example, Easytrieve Plus compares alphanumeric fields using the length of the first argument, whereas COBOL considers the length of both arguments. When converting existing Easytrieve Plus programs, you should perform several parallel runs to ensure the output is the same.

Example:


 FIELDA   W     4  A  VALUE '1234'
 FIELDB   W     6  A  VALUE '123456'

 IF FIELDA = FIELDB
     DISPLAY 'FIELDS MATCH'
 END-IF

In Easytrieve Plus, the IF statement results in a true outcome while in COBOL it is false as 1234 is not equal to 123456.

IMU issues an MNOTE message to alert the user of such potential problem.

If the output produced by IMU does not match the output produced by Easytrieve Plus, check IMU translator listing (SYSLIST1) for potential MNOTEs and change program to comply with the COBOL rules.

The table below lists allowed Relational and Logical Operators, allowed in Easytrieve, and their equivalent in COBOL as translated by Migration Utility.

  Easytrieve     COBOL             Explanation

       EQ           =               Test for Equal condition
       =            =

       NE           NOT =           Test for Not Equal condition
       NQ           NOT =
       *=           NOT =

       LT           <               Test for Less Than condition
       LS           <
       <            <

       *<           NOT <           Test for Not Less Than condition

       LE           NOT >           Test for Not Greater condition
       LQ           NOT >
       <=           NOT >
 

       GT           >               Test for Greater Than condition
       GR           >
       >            >
       *>           NOT >

       GE           NOT <           Test for Greater or Equal condition
       GQ           NOT <
       >=           NOT <

       OR           OR              Logical Operator OR

       AND          AND             Logical Operator AND

       NOT          NOT             Logical Operator NOT

       AND NOT      AND NOT         Logical Operator AND NOT

       OR  NOT      OR NOT          Logical Operator OR NOT

Examples

Here are some examples of IF and END-IF statements:

  FILE FILEIN1
  I-BALANCE 1   5  N 2

  WS-AMOUNT W   5  N 2

  JOB INPUT FILEIN1
  WS-AMOUNT = 0
  IF I-BALANCE > 5000                                |  Non-nested
     WS-AMOUNT = I-BALANCE * 1.10                    |
  ELSE                                               |
     WS-AMOUNT = I-BALANCE * 1.09                    |  IF statement
  END-IF

  IF I-BALANCE > (WS-AMOUNT + 55)
     WS-AMOUNT = I-BALANCE                           |  Arithmetic in
  ELSE                                               |
     WS-AMOUNT = I-BALANCE * 1.55                    |  IF statement
  END-IF

  IF I-BALANCE NOT NUMERIC
     DISPLAY 'BALANCE NOT NUMERIC'                   |
     DISPLAY HEX I-BALANCE                           |
   ELSE                                              |  Nested
     IF I-BALANCE EQ 5000, 5500, 5200                |
        WS-AMOUNT = I-BALANCE * 1.10                 |
     ELSE                                            |   IF statements
         WS-AMOUNT = I-BALANCE * 1.09                |
     END-IF                                          |
  END-IF