Conditional Processing - IF Statement

With the IF statement, you define a logical condition, and the execution of the statement attached to the IF statement then depends on that condition.

This document covers the following topics:


Structure of IF Statement

The IF statement contains three components:

IF In the IF clause, you specify the logical condition which is to be met.
THEN In the THEN clause you specify the statement(s) to be executed if this condition is met.
ELSE In the (optional) ELSE clause, you can specify the statement(s) to be executed if this condition is not met.

So, an IF statement takes the following general form:

IF condition  
   THEN execute statement(s)  
   ELSE execute other statement(s)  
END-IF

Note:
If you wish a certain processing to be performed only if the IF condition is not met, you can specify the clause THEN IGNORE. The IGNORE statement causes the IF condition to be ignored if it is met.

Example 1:

** Example 'IFX01': IF
************************************************************************
DEFINE DATA LOCAL
1 MYVIEW VIEW OF EMPLOYEES
  2 NAME
  2 BIRTH
  2 CITY
  2 SALARY (1:1)
END-DEFINE
*
LIMIT 7
READ MYVIEW BY CITY STARTING FROM 'C'
 IF SALARY (1) LT 40000 THEN
    WRITE NOTITLE '*****' NAME 30X 'SALARY LT 40000'
  ELSE
    DISPLAY NAME BIRTH (EM=YYYY-MM-DD) SALARY (1)
  END-IF
END-READ
END

The IF statement block in the above program causes the following conditional processing to be performed:

  • IF the salary is less than 40000, THEN the WRITE statement is to be executed;

  • otherwise (ELSE), that is, if the salary is 40000 or more, the DISPLAY statement is to be executed.

Output of Program IFX01:

        NAME            DATE      ANNUAL
                         OF       SALARY
                       BIRTH
-------------------- ---------- ----------
 
***** KEEN                                              SALARY LT 40000
***** FORRESTER                                         SALARY LT 40000
***** JONES                                             SALARY LT 40000
***** MELKANOFF                                         SALARY LT 40000
DAVENPORT            1948-12-25      42000
GEORGES              1949-10-26     182800
***** FULLERTON                                         SALARY LT 40000

Example 2:

** Example 'IFX03': IF
************************************************************************
DEFINE DATA LOCAL
1 EMPLOY-VIEW VIEW OF EMPLOYEES
  2 NAME
  2 CITY
  2 BONUS  (1,1)
  2 SALARY (1)
*
1 #INCOME  (N9)
1 #TEXT    (A26)
END-DEFINE
*
WRITE TITLE '-- DISTRIBUTION OF CATALOGS I AND II --' /
*
READ (3) EMPLOY-VIEW BY CITY = 'SAN FRANSISCO'
  COMPUTE #INCOME = BONUS(1,1) + SALARY(1)
  /*
 IF #INCOME > 40000
    MOVE 'CATALOGS I AND II' TO #TEXT
  ELSE
    MOVE 'CATALOG I'         TO #TEXT
  END-IF
  /*
  DISPLAY NAME 5X 'SALARY' SALARY(1) / BONUS(1,1)
  WRITE T*SALARY '-'(10) /
        16X 'INCOME:' T*SALARY #INCOME 3X #TEXT /
        16X '='(19)
  SKIP 1
END-READ
END

Output of Program IFX03:

                    -- DISTRIBUTION OF CATALOGS I AND II --
        NAME               SALARY
                           BONUS
--------------------     ----------
 
COLVILLE JR                   56000
                                  0
                         ----------
                INCOME:       56000   CATALOGS I AND II
                ===================
 
RICHMOND                       9150
                                  0
                         ----------
                INCOME:        9150   CATALOG I
                ===================
 
MONKTON                       13500
                                600
                         ----------
                INCOME:       14100   CATALOG I
                ===================

Nested IF Statements

It is possible to use various nested IF statements; for example, you can make the execution of a THEN clause dependent on another IF statement which you specify in the THEN clause.

Example:

** Example 'IFX02': IF (two IF statements nested)
************************************************************************
DEFINE DATA LOCAL
1 MYVIEW VIEW OF EMPLOYEES
  2 NAME
  2 CITY
  2 SALARY (1:1)
  2 BIRTH
  2 PERSONNEL-ID
1 MYVIEW2 VIEW OF VEHICLES
  2 PERSONNEL-ID
  2 MAKE
*
1 #BIRTH   (D)
END-DEFINE
*
MOVE EDITED '19450101' TO #BIRTH (EM=YYYYMMDD)
*
LIMIT 20
FND1. FIND MYVIEW WITH CITY = 'BOSTON'
                  SORTED BY NAME
 IF SALARY (1) LESS THAN 20000
    WRITE NOTITLE '*****' NAME 30X 'SALARY LT 20000'
  ELSE
    IF BIRTH GT #BIRTH
      FIND MYVIEW2 WITH PERSONNEL-ID = PERSONNEL-ID (FND1.)
        DISPLAY (IS=ON) NAME BIRTH (EM=YYYY-MM-DD)
                        SALARY (1) MAKE (AL=8 IS=OFF)
      END-FIND
    END-IF
  END-IF
  SKIP 1
END-FIND
END

Output of Program IFX02:

        NAME            DATE      ANNUAL     MAKE
                         OF       SALARY
                       BIRTH
-------------------- ---------- ---------- --------
 
***** COHEN                                             SALARY LT 20000
 
CREMER               1972-12-14      20000 FORD
 
***** FLEMING                                           SALARY LT 20000
 
PERREAULT            1950-05-12      30500 CHRYSLER
 
***** SHAW                                              SALARY LT 20000
 
STANWOOD             1946-09-08      31000 CHRYSLER
                                           FORD