Implied IFs

Implied IF statements are a way of testing specific conditions without actually using an IF. Implied IFs are implemented in clauses associated with one of the COBOL verbs.

At end:

The most frequently used implied IF is the AT END statement on the READ. The AT END clause is asking the question, is it end of file and if it is, the command or commands following the AT END will be executed. There is also a NOT AT END clause that can be used with the READ to specify processing that is to take place if the file has not reached end of file. There is also an optional END-IF that can be used to terminate the READ statement without a period. If you ever need to imbed the READ statement in an IF statement, the END-READIF becomes very important because you can terminate the READ statement with out terminating the IF.

Example:

            IF RECORD-CT NOT  1000	
		READ INPUT-FILE
		    AT END
			MOVE "YES" TO EOF-IND
		    NOT AT END
			ADD 1 TO RECORD-CT
                    END-READ
            ELSE
		 PERFORM B-210-PRINT-RESULTS-ROUT.

On size error:

ON SIZE ERROR is an implied IF clause used with a calculation statement: ADD, SUBTRACT, MULTIPLY, DIVIDE and COMPUTE. The ON SIZE ERROR clause will be triggered when the answer the size of the answer field would cause the loss of significant digits or when the program is attempting to divide by 0. For example, if the answer was calculated as 5678 and the picture of the answer field was 999, a size error would occur, however if the answer was 345V678 and the answer field was 999V99, no size error would be generated because the 8 in the thousands decimal position is not considered a significant digit. As with other COBOL implied ifs, there is also a NOT ON SIZE ERROR which will be activated if the calculation has resulted in a usable answer. In addition, it should be noted that all of the calculation statement also come with end statements (END-ADD, END-SUBTRACT, END-MULTIPLY, END-DIVIDE, and END-COMPUTE) so that the implied ifs can be terminated with either a period or the end statement. This is extremely important if the calculation statement is being embedded into an IF statement, because the period that terminated the calculation statement would also terminate the END-IF.

Example #1:

    
                    ADD AMT TO 1000
		        GIVING ANS-WS
                            ON SIZE ERROR
                                MOVE "INVALID" TO MSG-PR.

OR some programmers prefer to line the ON SIZE with the GIVING

                    ADD AMT TO 1000
		        GIVING ANS-WS
                        ON SIZE ERROR
                            MOVE "INVALID" TO MSG-PR.

Example #2:

                    ADD AMT TO 1000
		        GIVING ANS-WS
                            ON SIZE ERROR
                                MOVE "INVALID" TO MSG-PR
                            NOT ON SIZE ERROR
                                MOVE ANS-WS TO ANS-PR.

In this example it can be seen that the NOT clause performs in a similar manner to the ELSE clause in the standard IF. If there is size error the invalid message will be moved, else the answer will be moved to the printline.

Example #3:

                    IF CODEZ = "A"
                        MOVE ITEM-ID TO ITEM-ID-PR
                            ADD AMT TO 1000
                                GIVING ANS-WS
                                   ON SIZE ERROR
                                       MOVE "INVALID" TO MSG-PR
                                   NOT ON SIZE ERROR
                                       MOVE ANS-WS TO ANS-PR
                            END-ADD
                        MOVE  ITEM-NAME TO ITEM-NAME-PR
                     ELSE
                        IF CODEZ = "B"
                            ...

As can be seen in this example, the ADD with the size error clauses is embedded in an IF. The last on size error clause needs to be terminated or all statements following would be taken as part of NOT ON SIZE ERROR. However, if the ADD was terminated with a period the IF would also be terminated. The solution is the END-ADD statement which terminates the ADD and its SIZE ERROR clauses without terminating the IF.

All COBOL statements that have implied ifs associated with them, also have END statements so that the verb can be terminated by using the END associated with that verb or by using the period.