Previous Topic: FOR NEW Statement (VSAM Files)Next Topic: INITIATE Statement


IF Statement

The IF statement chooses one of two alternative courses of action, depending on whether a condition is true, false, or unknown.

This statement has the following format:

IF condition
[THEN         ]
[   statements]
[ELSE        ]
[   statement]
ENDIF
condition

A user‑defined condition (see the definition of a condition in chapter one). Statements that immediately follow the IF [THEN] condition are executed only if the condition is true.

THEN

A reserved word that you can add for readability.

ELSE

Marks the start of a set of statements to execute if the condition is False or Unknown. If you omit ELSE and the condition is False or Unknown, the IF statement does not cause any action and the next statement after the ENDIF executes.

ENDIF

Terminates the IF construct. When IF statements are nested, the most recent unterminated IF construct is terminated by the first occurrence of ENDIF. Each IF in a nested IF construct must have a corresponding ENDIF.

Examples

IF QUANT_ON_HAND > QUANT_ORDERED
   SUBTRACT QUANT_ORDERED
     FROM QUANT_ON_HAND
ELSE
MOVE "OUT OF STOCK" TO MESSAGE
   PRODUCE EX_LINE
ENDIF
IF NOT SUFF_ON_HAND
    DO REORDER_ITEM
ENDIF
IF (EMP_DEPT = 'D' AND JOB_CODE = 'J')
   OR RECENTLY_HIRED
      DO PROCESS_JUNIOR
ENDIF
<<MAIN>> PROCEDURE
FOR EACH PAYROLL
   WHERE YTD_COMMISSION > 7500
      FOR EMPLOYEE
         WHERE NUMBER = PAYROLL.NUMBER
         SET W_YTD_NET = (YTD_WAGES + YTD_COMMISSION)
         IF ACTIVITY_CODE = 'A'                       :ACTIVE
            IF ACTIVITY_STATUS = 'S'                  :S=SALARIED
               SET W_TAG = 'SALARIED'
            ELSE                                      :H=HOURLY
               SET W_TAG = 'HOURLY'
            ENDIF
         ELSE                                         :INACTIVE
            SET W_TAG = 'INACTIVE'
         ENDIF
      LIST EMPLOYEE.NUMBER EMPLOYEE.NAME
         W_YTD_NET  W_TAG
      WHEN NONE
      ENDFOR
ENDFOR
ENDPROC