IF/ELSE-IF/ELSE

Evaluate a condition.
Restriction: This topic applies only when the AppMaster Builder AddPack has been installed, and applies only to Windows platforms.

Syntax: for Format 1

 IF condition1
    statementblock
[ELSE-IF|ELSE IF condition1
    statementblock
              .
              .
              .
 ELSE-IF|ELSE IF conditionN
    statementblock ]
[ELSE
    statementblock ]

Syntax: for Format 2

COBOLimperativestatement
... COBOLconditionalclause
    statementblock
ELSE-IF|ELSE IF condition1
    statementblock
[             .
              .
              .
ELSE-IF|ELSE IF conditionN
    statementblock ]
[ELSE
    statementblock ]

General Rules:

  1. When IF condition1 is true, its statement block executes. Control then passes to the next statement at the same or less indented level as the IF statement (other than related ELSE-IF or ELSE statements which, by definition, are at the same level as their IF).
  2. When IF condition1 is false, S-COBOL looks for ELSE-IF statements at the same level. If such ELSE-IF statements are present, the conditions are evaluated one after the other. If one of the conditions is true, its statement block executes. Control then passes to the next statement at the same or less indentation as the IF statement (other than related ELSE-IF or ELSE statements).
  3. If no ELSE-IF statements are present, or if the IF condition and the ELSE-IF conditions are all false, control passes to the statement block subordinate to the ELSE statement; if no ELSE statement is present, control passes to the next statement at the same or less indentation as the IF and ELSE-IF statements.
  4. If an ELSE-IF statement has no subordinate statement block and the condition is true, control passes to the next statement at the same or less indentation level as the ELSE-IF statement.
  5. You can nest up to fourteen IF levels.
  6. The last IF, ELSE-IF, or ELSE coded in a related series requires a subordinate statement block.
  7. With Format 2, you can form conditional constructions similar to IF, ELSE-IF, ELSE by combining a COBOL imperative verb and its conditional clauses with ELSE-IF and ELSE statements. Verbs that permit this construction are:
    ADD, RETURN, CALL, REWRITE, COMPUTE, START, 
    DELETE, STRING, DIVIDE, SUBTRACT, MULTIPLY, 
    UNSTRING, READ, WRITE.
    
  8. You can use the IF construct to code the NEXT SENTENCE concept of passing program control out of a particular construct to the next executable statement. See "Example".
  9. AT END and INVALID KEY conditional clauses can be combined with ELSE-IF and ELSE statements.

Example:

If line 2020 is false, pass control back to line 1020, the first statement after the PERFORM statement, because there is no ELSE-IF or ELSE coding associated with this IF, and the first character at the same or less indentation as this IF is a new paragraph name, which denotes the end of the preceding PERFORMed paragraph. If the line 2020 condition is true, execute its subordinate statement block (lines 2030 through 2170) and return control to line 1020.

After line 2030 is executed, test line 2040. If the condition in line 2040 is true, execute lines 2050 through 2071; pass control to line 2170. If line 2040 is false, test line 2080, etc. If lines 2040, 2080, and 2120 are false, execute the ELSE statement block (line 2160) and pass control to line 2170.
001010         PERFORM EMPLOYEE-BENEFIT-DEDUCTION
 001020             MOVE ...
                      .
                      .
 002010  PARA   EMPLOYEE-BENEFIT-DEDUCTION
 002020         IF EMPL-COVERAGE NOT = SPACES
 002030             PERFORM CALC-BASIC-BEN
 002040             IF EMPL-COVERAGE-TYPE = 'EXTRA'
 002050                 PERFORM CALC-EXTRA-BEN
 002060                 PERFORM CALC-DENTAL-BEN
 002070                 BEN-FIELD = XTRA-BEN + 
 002071                 ... DENTAL-BEN
 002080             ELSE-IF EMPL-COVERAGE-TYPE = 
 002081             ... 'FAMILY'
 002090                 PERFORM CALC-FAMILY-BEN
 002100                 PERFORM CALC-DENTAL-BEN
 002110                 BEN-FIELD = FAMILY-BEN + 
 002111                 ... DENTAL-BEN
 002120             ELSE-IF EMPL-COVERAGE-TYPE = 
 002121             ... 'DENTAL'
 002130                 PERFORM CALC-DENTAL-BEN
 002140                 BEN-FIELD = BASIC-BEN + 
 002141                 ... DENTAL-BEN
 002150             ELSE
 002160                 BEN-FIELD = BASIC-BEN
 002170             EMPL-DED-FIELD =
 002171             ... BEN-FIELD * .5 / 12
 002180
 002190     PARA  CALC-BASIC-BEN
Nest conditional statements.
IF condition1
    statementblock1
    IF condition2
        IF condition3
    statementblock2
        ELSE-IF condition4
        ELSE-IF condition5
    statementblock3
        ELSE
    statementblock4
    ELSE
    statementblock5
        IF condition6
            IF condition7
    statementblock6

    statementblock7
Make the MULTIPLY function conditional by ON SIZE ERROR.
            WS-NET-PAY = EMP-HOURS *
            ... EMP-HOURLY-RATE
            ... ON SIZE ERROR
                PERFORM PRINT-ERROR-MESSAGE
                DISPLAY SSNO WS-NET-PAY
                WS-NET-PAY  WS-DEDUC = 0
            ELSE-IF EMP-HOURLY-RATE = MIN-WAGE
                PERFORM CALC-DEDUC-MIN
            ELSE-IF EMP-HOURLY-RATE < 5.00
                PERFORM CALC-DEDUC-1
            ELSE-IF EMP-HOURLY-RATE >= 5.00
                PERFORM CALC-DEDUC-2
                IF EMP-HOURLY-RATE > 20.00
                    DISPLAY SSNO 
                    ... EMP-HOURLY-RATE
            ELSE
                DISPLAY SSNO  EMP-HOURLY-RATE
                PERFORM PRINT-ERROR-MESSAGE
            NET-PAY = WS-NET-PAY - WS-DEDUC
Using a NEXT SENTENCE construction, the following S-COBOL code:
 PARA   CALC-BENEFIT
            BEN-FIELD = ZERO
            IF PERM-PART-TIME
                PERFORM GROUP-A-CALC
                IF HRS-WORKED > 25
                    BEN-FIELD = 
                    ... BEN-FIELD * 1.25
            ELSE-IF PART-TIME
            ELSE-IF FULL-TIME
                PERFORM GROUP-B-CALC
            EMPL-REC-BEN-FIELD = 
            ... BEN-FIELD
Replaces the following COBOL code:
CALC-BENEFIT.
    MOVE ZERO TO BEN-FIELD.
    IF PERM-PART-TIME
        PERFORM GROUP-A-CALC
        IF HRS-WORKED > 25
            MULTIPLY BEN-FIELD BY 1.25
        ELSE
            NEXT SENTENCE
    ELSE
        IF PART-TIME
            NEXT SENTENCE
        ELSE
            IF FULL-TIME
                PERFORM GROUP-B-CALC
            ELSE
                NEXT SENTENCE.
    MOVE BEN-FIELD TO EMPL-REC-BEN-FIELD.