IF

Structured Mode Syntax

IF logical-condition
  [THEN] statement
  [ELSEstatement ]
END-IF

Reporting Mode Syntax

IF logical-condition
  [THEN]

statement

 
DO statement DOEND

ELSE

statement
DO statement DOEND

This document covers the following topics:

For an explanation of the symbols used in the syntax diagram, see Syntax Symbols.

Related Statements: DECIDE FOR | DECIDE ON | IF SELECTION | ON ERROR

Belongs to Function Group: Processing of Logical Conditions


Function

The IF statement is used to control execution of a statement or group of statements based on a logical condition.

Note:
If no action is to be performed in case the condition is met, you must specify the statement IGNORE in the THEN clause.

Syntax Description

Syntax Element Description
IF logical-condition
Logical Condition Criterion:

The logical condition which is used to determine whether the statement or statements specified with the IF statement are to be executed.

Examples:

IF #A = #B
IF LEAVE-TAKEN GT 30
IF #SALARY(1) * 1.15 GT 5000
IF SALARY (4) = 5000 THRU 6000
IF DEPT = 'A10' OR = 'A20' OR = 'A30'

For further information, see the section Logical Condition Criteria (in the Programming Guide).

THEN statement
THEN Clause:

In the THEN clause, you specify the statement(s) to be executed if the logical condition is true.

ELSE statement
ELSE Clause:

In the ELSE clause, you specify the statement(s) to be executed if the logical condition is not true.

END-IF
END of IF Statement:

In structured mode, the Natural reserved word END-IF must be used to end the IF statement.

In reporting mode, use the DO ... DOEND statements to supply one or several suitable statements, depending on the situation, and to end the clauses and the IF statement. If you specify only a single statement, you can omit the DO ... DOEND statements. With respect to good coding practice, this is not recommended.

statement
DO statement ... DOEND

Example

** Example 'IFEX1S': IF (structured mode)                               
************************************************************************
DEFINE DATA LOCAL                                                       
1 EMPLOY-VIEW VIEW OF EMPLOYEES                                         
  2 PERSONNEL-ID                                                        
  2 NAME                                                                
  2 FIRST-NAME                                                          
  2 SALARY (1)                                                          
  2 BIRTH                                                               
1 VEHIC-VIEW VIEW OF VEHICLES                                           
  2 PERSONNEL-ID                                                        
  2 MAKE                                                                
*                                                                       
1 #BIRTH (D)                                                            
END-DEFINE                                                              
*                                                                       
MOVE EDITED '19450101' TO #BIRTH (EM=YYYYMMDD)                          
SUSPEND IDENTICAL SUPPRESS                                              
LIMIT 20                                                                
*                                                            
FND. FIND EMPLOY-VIEW WITH CITY = 'FRANKFURT'                
          SORTED BY NAME BIRTH                               
  IF SALARY (1) LT 40000                                     
    WRITE NOTITLE '*****' NAME 30X 'SALARY LT 40000'         
  ELSE                                                       
    IF BIRTH GT #BIRTH                                       
      FIND VEHIC-VIEW WITH PERSONNEL-ID = PERSONNEL-ID (FND.)
        DISPLAY (IS=ON)                                      
                NAME BIRTH (EM=YYYY-MM-DD)                   
                SALARY (1) MAKE (AL=8)                       
      END-FIND                                               
    END-IF                                                   
  END-IF                                                  
END-FIND                                                     
END

Output of Program IFEX1S:

        NAME            DATE      ANNUAL     MAKE                      
                         OF       SALARY                               
                       BIRTH                                           
-------------------- ---------- ---------- --------                    
                                                                       
BAECKER              1956-01-05      74400 BMW                         
***** BECKER                                            SALARY LT 40000
BLOEMER              1979-11-07      45200 FIAT                        
FALTER               1954-05-23      70800 FORD                        
***** FALTER                                            SALARY LT 40000
***** GROTHE                                            SALARY LT 40000
***** HEILBROCK                                         SALARY LT 40000
***** HESCHMANN                                         SALARY LT 40000
HUCH                 1952-09-12      67200 MERCEDES                    
***** KICKSTEIN                                         SALARY LT 40000
***** KLEENE                                            SALARY LT 40000
***** KRAMER                                            SALARY LT 40000

Equivalent reporting-mode example: IFEX1R.