IF Statement


The IF statement is a powerful programming tool that allows selective branching within a program. Using the IF statement, the programmer can set up code that will only be processed under specified circumstances. The use of the IF statment is closely tied TO the use of logic tools like flowcharting because of the complexity of the problems that the IF statement allows. The flowchart provides an excellent vehicle to think through the logical problem prior to writing the COBOL code.

Simple IF:

SYNTAX:

    IF {condition}
        statement(s) to execute.


The simple IF statement tests a condition. If the condition is true, than a statement or statements are executed. In the simple IF shown above, if the condition is not true nothing is done. When the IF has been executed, control passes to the next statement in the program. The statement(s) to be executed may be one statement which would be written in the box or a series of statements which could be shown on the flowchart as a series of boxes each containing a statement to be executed. Frequently, if there is a lengthy series of statements instead of including them all in the IF statement, the statement to be executed is coded as a PERFORM statement. This means that if the condition is true, the PERFORM statement is executed. The PERFORM statement goes to the paragraph it is told to perform, executes all of the statements/instructions in that paragraph and when the paragraph is complete returns to the IF. If the PERFORM is the only statement in the IF, then the IF is done and control moves to the statement below the IF.

The condition can be expressed in a variety of ways but the format requires that two things be compared - they can be field names or literals. (Note: Alphanumeric literals should be enclosed in quotes and numeric literals should not be enclosed in quotes.) The two fields or literals are compared using a relational operator that establishes the relationship between the two things being compared. The equal sign, the greater than sign and the less than sign are all examples of relational operators. In setting up the condition, the optional NOT can be used with any of the relational operators. For readability, COBOL also allows the use of other words like IS, THAN, and TO. In the following examples, the required words are underline and the optional words are enclosed in square brackets.

Valid relational operators are:
IS [NOT] EQUAL TO
IS [NOT] =
IS [NOT] GREATER THAN
IS [NOT] >
IS [NOT] LESS THAN
IS [NOT] <
IS GREATER THAN OR EQUAL TO
IS >=
IS LESS THAN OR EQUAL TO
IS <=
Example #1:I want to count the number of full time employees. On my employee file, full time employees are designated with a F in the column called TYPE-EMP. Notice that the IF is terminated with a period (later in this handout we will look at END-IF as an alternative).

    IF TYPE-EMP = "F"
        ADD 1 TO EMP-CT.







Example #2:If the employee is a full time employee there are a series of statements or instructions to be executed (I need to add to a counter, move a message to the printline and move the salary to the printline). To do this I will need to execute multiple instructions after the IF. Remember, a period terminates the IF, so none of my instructions that need to be done if the statement is true can have a period. If this is a problem, the END-IF to be discussed later offers some possibilities as does the method discribed in Example #3 below. Note that after the final MOVE statement there is a period because at that point, I am terminating the IF.

    IF TYPE-EMP = "F"
        ADD 1 TO FULL-CT
        MOVE "FULL TIME" TO MSG-PR
        MOVE SALARY TO PAY-PR.








Example #3:If the employee is a full time employee there are a series of statements or instructions to be executed. These instructions will be in the B-300-FULL-TIME paragraph. The IF statement will move to B-300-FULL-TIME and the instructions will be executed. When the paragraph is done, control returns to the IF statement. Since there is nothing more in the IF statement, control moves to the statement below the IF statement.

    IF TYPE-EMP = "F"
        PERFORM B-300-FULL-TIME.
    ...
    ...
    B-300-FULL-TIME.
        ADD 1 TO FULL-CT.
        MOVE "FULL TIME" TO MSG-PR.
        MOVE SALARY TO PAY-PR.

ELSE clause on the simple if:

The simple IF can have an ELSE clause that direct processing if the condition is FALSE.. Any statement(s) below the ELSE clause will be executed if the condition is FALSE.

SYNTAX:

    IF {condition}
        statement1...
    ELSE
        statement2....



Example #4:In this example, if the employee is full time I want to add 1 to FULL-CT otherwise I want to add 1 to OTHER-CT. Notice that there is no period until the end of the entire IF...ELSE structure.
    IF TYPE-EMP = "F"
        ADD 1 TO FULL-CT
    ELSE
        ADD 1 TO OTHER-CT.




Example #5:In this example, I have multiple things to do if the statement is true and multiple things to do if the statement is false. All of the code can be incorporated into the IF statement as shown in this example, or the PERFORM statements can be used as shown in example #6.


    IF TYPE-EMP = "F"
        ADD 1 TO FULL-CT
        MOVE "FULL TIME" TO MSG-PR
        MOVE SALARY TO PAY-PR
    ELSE
        ADD 1 TO OTHER-CT
        MOVE "NOT FULL TIME" TO MSG-PR
        MULTIPLY PAY-HR BY HOURS
            GIVING GROSS-WS
        MOVE GROSS-WS TO PAY-PR.


Example #6:


    IF TYPE-EMP = "F"
        PERFORM B-300-FULL-TIME
    ELSE
        PERFORM B-310-NOT-FULL-TIME.
...
...
B-300-FULL-TIME.
    ADD 1 TO FULL-CT
    MOVE "FULL TIME" TO MSG-PR
    MOVE SALARY TO PAY-PR.
B-310-NOT-FULL-TIME.
    ADD 1 TO OTHER-CT
    MOVE "NOT FULL TIME" TO MSG-PR
    MULTIPLY PAY-HR BY HOURS
        GIVING GROSS-WS
    MOVE GROSS-WS TO PAY-PR.

In COBOL '85, the END-IF clause was added to the IF statement, presenting an alternative to the period for terminating the IF. Its value is not evident with the simple IF, but with compounded and embedded IF statements it becomes a powerful and convenient tool. Examples #1 through #6 are redone below using the END-IF clause. Note that in these examples the period after the END-IF is optional.


SYNTAX:     IF {condition}
        statement1...
    ELSE
        statement2...
    END-IF

EXAMPLES:

Example #1      IF TYPE-EMP = "F"
    ADD 1 TO EMP-CT
END-IF.
Example #2 IF TYPE-EMP = "F"
    ADD 1 TO FULL-CT
    MOVE "FULL TIME" TO MSG-PR
    MOVE SALARY TO PAY-PR
END-IF.
Example #3 IF TYPE-EMP = "F"
    PERFORM B-300-FULL-TIME
END-IF.
Example #4 IF TYPE-EMP = "F"
    ADD 1 TO FULL-CT
ELSE
    ADD 1 TO OTHER-CT
END-IF.
Example #5 IF TYPE-EMP = "F"
    ADD 1 TO FULL-CT
    MOVE "FULL TIME" TO MSG-PR
    MOVE SALARY TO PAY-PR
ELSE
    ADD 1 TO OTHER-CT
    MOVE "NOT FULL TIME" TO MSG-PR
    MULTIPLY PAY-HR BY HOURS
        GIVING GROSS-WS
    MOVE GROSS-WS TO PAY-PR
END-IF.
Example #6 IF TYPE-EMP = "F"
    PERFORM B-300-FULL-TIME
ELSE
    PERFORM B-310-NOT-FULL-TIME
END-IF.

Use of next sentence:

The NEXT SENTENCE clause is used when there is no action to be taken. It transfers control to the next sentence in the program.

SYNTAX:
    IF {condition}
        statement1... or
        NEXT SENTENCE
    ELSE
        statement2... or
        NEXT SENTENCE
[END-IF]

NOTE: If the condition is true you can either execute one or more statements or use the clause NEXT SENTENCE and if the condition is false you can either execute one or more statements or use the statement NEXT SENTENCE. The END-IF is optional. The programmer can use either the END-IF or the period to terminate the IF statement.

Example #7:If the field called AMT does not contain a value greater than 100, the program should print a message saying AMT TOO LOW.


IF AMT > 100
    NEXT SENTENCE
ELSE
    MOVE "AMT TOO LOW" TO MSG-PR.

In this example, if the AMT is greater than 100 no processing is done and control passes to the next sentence after the IF. If the AMT is not greater than 100, the ELSE will be executed and the message will be moved. Alternative approachs are to use the NOT clause in the condition or phase the question differently. Example #8 shows three possible alternatives:

Example #8:Three posssible ways of coding whether or not AMT is not over 100 are illustrated.


IF AMT NOT > 100
    MOVE "AMT TOO LOW" TO MSG-PR.



IF AMT <= 100
    MOVE "AMT TOO LOW" TO MSG-PR.



IF AMT < 101
    MOVE "AMT TOO LOW" TO MSG-PR.

Nested IF:

The NESTED IF allows the programmer to group a variety of different questions in a structure that will allow the processing of all possible results. The conditions being tested can be in an AND relationship or an OR relationship or a combination. The ELSE can be used to provide false processing for each of the IF statements. When using NESTED IF statements, the IF verb is repeated with each condition being tested. When two conditions are in an AND relationship, both conditions must be true for the entire IF statement to be true. When two conditions are in an OR relationship, then if either one of the conditions is true, the IF statement is true. The following examples will demonstrate the use of the NESTED IF.

Example #9:

SIMPLE AND:In this example, if the SALARY is over 50000 AND the TYPE-EMP is equal to F then 1 should be added to OVER-CT. The first condition is tested, if it is true than the second condition is tested. If that is true the ADD is executed. Only if both conditions are true will the ADD be executed. If either condition is false than the IF statement ends and processing moves to the next sentence.


IF SALARY > 50000
    IF TYPE-EMP = "F"
        ADD 1 TO OVER-CT.


Alternative:

IF SALARY > 50000
    IF TYPE-EMP = "F"
        ADD 1 TO OVER-CT
    END-IF
END-IF.


EXAMPLE #10:

SIMPLE OR:In this example, if the SALARY is over 50000 OR the TYPE-EMP is equal to F then 1 should be added to OVER-CT. This is an EITHER...OR test. If the first condition tests true than the ADD is executed and the IF ends. If the first condition tests false than processing drops to the ELSE where there is another condition which is essentially a second chance. If the second IF tests true than the ADD is executed. If not, the program has run out of chances and the IF statement terminates.


IF SALARY > 50000
    ADD 1 TO OVER-CT
ELSE
    IF TYPE-EMP = "F"
        ADD 1 TO OVER-CT.

Alternative:

IF SALARY > 50000
    ADD 1 TO OVER-CT
ELSE
    IF TYPE-EMP = "F"
        ADD 1 TO OVER-CT
    END-IF
END-IF.

Combined AND, Combined OR:

Instead of a NESTED IF, the programmer can use the combined IF statement to accomplish the same thing. The COMBINED IF puts both questions in one IF statement. Let's relook at the previous two examples and see how they would be done with a COMBINED IF statement.

EXAMPLE #9 USING COMBINED IF:

    IF SALARY > 50000 AND TYPE-EMP = "F"
        ADD 1 TO OVER-CT.

As in the previous version of example #9, the ADD 1 TO OVER-CT will only be executed if both conditions in the combined condition statement are true. If either of them is false, no processing will be done.

EXAMPLE #10 USING COMBINED IF:

    IF SALARY > 50000 OR TYPE-EMP = "F"
        ADD 1 TO OVER-CT.

As in the previous version of example #10, the ADD 1 TO OVER-CT will be executed if EITHER of the conditions are true. Unless both conditions are false, the ADD will be executed.

Using combination of and with or

When you combine AND with OR, and the possibility of NOT as well, you need to understand the evaluation or resolution hierarchy that COBOL uses.

  1. Evaluation proceeds from left to right.
  2. If parenthesis are used, they will be resolved first which means that evaluation will start with the resolution of the contents of the inner most parenthesis first and continue till the contents of all parenthesis have been evaluated.
  3. If the NOT is used, it will be evaluated next.
  4. AND and the conditions around it are evaluated next.
  5. Finally, OR and the conditions around it are evaluated.
In summary, this means that ANDs are resolved before ORs. If you want to change this order of operation, you need to use parenthesis around the OR and its conditions so it will be evaluated first.

EXAMPLE #11:This example wants to count all inventory items (ADD 1 TO CT-WS) with CLASS-CODE equal to B AND where EITHER the ON-HAND is greater than 100 OR the ON-ORDER is greater than 100. Lets look at the possibilities:

CLASS-CODE = "B" AND
ON-HAND >100 OR
ON-ORDER > 100
Since AND is resolved before OR, this would read as CLASS-CODE = "B" AND ON-HAND > 100 OR just ON-ORDER > 100 - Not what we want!
CLASS-CODE = "B" AND
(ON-HAND > 100 OR
ON-ORDER > 100)
Since parenthesis resolve first, this will combine the CLASS-CODE = "B" with either of the other - Exactly what we want!

COMBINED method(flowchart applies):

IF CLASS-CODE = "B" AND (ON-HAND > 100 OR ON-ORDER > 100)
    ADD 1 TO CT-WS.

NESTED method(flowchart applies):

IF CLASS-CODE = "B"
    IF ON-HAND > 100
        ADD 1 TO CT-WS
    ELSE
        IF ON-ORDER > 100
            ADD 1 TO CT-WS.
NESTED method with END-IF(flowchart applies):

IF CLASS-CODE = "B"
    IF ON-HAND > 100
        ADD 1 TO CT-WS
    ELSE
        IF ON-ORDER > 100
            ADD 1 TO CT-WS
        END-IF
    END-IF
END-IF

Value of the END-IF

The END-IF statement can become very important in a nested IF with a variety of conditions to be evaluated. Suppose, for example, that we are calculating payroll. If the employee is full time, we want to check and see if they are salaried. If they are salaried we will use salary as their gross pay. If they are not salaried, then we need to calculate their gross pay by multiplying pay per hour by hours worked. The gross pay for full time workers should be added to the total full time salary accumulator. If the employee is not full time we need to multiply their pay per hour by hours worked and add their gross pay to the not full time salary accumultor.

The flowchart for the problem is:

Now, lets look at the code using a nested IF statement:

IF TYPE-EMP = "F"
    IF PAY-CODE = "S"
        MOVE SALARY TO GROSS-WS
    ELSE
        MULTIPLY PAY-HR BY HOURS
            GIVING GROSS-WS
    END-IF
    ADD GROSS-WS TO TOTAL-FULL-TIME-ACC
ELSE
    MULTIPLY PAY-HR BY HOURS
        GIVING GROSS-WS
    ADD GROSS-WS TO TOTAL-NOT-FULL-TIME-ACC.

Notice the use of the END-IF to terminate the question about PAY-CODE while still keeping the question about TYPE-EMP open. This lets the programmer add to the accumulator. Without the END-IF the ADD would only apply to non salaried full time workers. The alternative of using a period instead of the END-IF would not work because a period would terminate the entire IF statement.

NEXT SENTENCE VS. CONTINUE

COBOL '85 has added the CONTINUE clause to the IF statement. It works in a similiar manner to the NEXT SENTENCE, but there are subtle differences. When NEXT SENTENCE is encountered, control passes out of the IF to the next instruction. When the CONTINUE is encountered, control passes out of the IF to the next instruction unless there is an END-IF in which case control passes to the statement after the END-IF.

Suppose that we have an inventory problem. If the number on hand is less than the reorder point, we want to check and see if there are items on order. If ON-ORDER is 0 we want to perform a routine to prepare the order otherwise we have nothing special to do. However, if the number on hand is less than the reorder point, irregardless of the ON-ORDER status, we want to move a message to the printline. The logic flowchart for this problem is:


IF ON-HAND < REORDER-PT
    IF ON-ORDER = 0
        PERFORM B-300-PREPARE-ORDER
    ELSE
        CONTINUE
    END-IF
    MOVE "ON HAND LOW" TO MSG-PR.

The CONTINUE serves an important purpose here. If NEXT SENTENCE had been used instead of CONTINUE, the MOVE statement would never have been executed.