Chapter 9 – More on Procedure Division • PERFORM – TEST BEFORE/TEST AFTER – Inline Perform • not calling a procedure – Performing Sections • can be a paragraph or a section – paragraph - one or more sentances – section - one or more paragraphs Figure 9.1 The Iteration Structure A TRUE CONDITION? A FALSE CONDITION? FALSE TRUE (a) DO WHILE Construct (b) DO UNTIL Construct Figure 9.2 Structure of a COBOL Program READ INPUT-FILE AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH. PERFORM PROCESS-RECORDS UNTIL DATA-REMAINS-SWITCH = ‘NO’. PROCESS-RECORDS. . Procedure Division statements to process the current record . . READ INPUT-FILE AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH. (a) Priming Read PERFORM UNTIL DATA-REMAINS-SWITCH = ‘NO’ READ INPUT-FILE AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH NOT AT END . Procedure Division statements to process the current record . . END-READ END-PERFORM. (b) False Condition Branch with In-Line Perform • PERFORM THRU – executes statements between procedure names. – PERFORM PROCESS1 THRU PROCESS4 • READ – False Condition Branch • NOT AT END • way to avoid the priming read – READ INTO • read data into 2 places, working storage and the I/O area. • WRITE FORM – same as READ INTO but used for writing a record. WRITE HEADING-LINE FROM WS-PRINT-LINE = MOVE WS-PRINT-LINE TO HEADING-LINE WRITE HEADING-LINE • String Processing – INSPECT - find/replace – STRING - joins strings – UNSTRING - splits strings Figure 9.3 The READ INTO statement FD EMPLOYEE-FILE DATA RECORD IS EMPLOYEE-RECORD. 01 EMPLOYEE-RECORD PIC X(60). . . . WORKING-STORAGE SECTION. 01 FILLER PIC X(14) VALUE ‘WS BEGINS HERE’. 01 WS-EMPLOYEE-RECORD. 05 EMP-NAME PIC X(25). . . . PROCEDURE DIVISION. . . . READ EMPLOYEE-FILE INTO WS-EMPLOYEE-RECORD AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH NOT AT END PERFORM PROCESS-THIS-RECORD END-READ. Figure 9.4 The INSPECT statement 01 RECORD-IN. 05 SOC-SEC-NUM . . . 01 PRINT-LINE. 05 SOC-SEC-NUM-OUT . . . PIC 9(9). PIC 999B99B9999. PROCEDURE DIVISION. . . . MOVE SOC-SEC-NUM TO SOC-NUM-OUT. INSPECT SOC-SEC-NUM-OUT REPLACING ALL ‘ ‘ BY ‘-’. Initial: SOC-SEC-NUM 123 45 6789 Result: SOC-SEC-NUM-OUT 123-45-6789 Figure 9.5 The STRING Statement 05 NAME-IN-PIECES. 10 LAST-NAME 10 FIRST-NAME 10 MIDDLE-INITIAL PIC X(16). PIC X(10). PIC X. 05 ENTIRE-NAME PIC X(29). (a) Working-Storage Holding Areas Before Execution LAST-NAME SM I T H MIDDLE-INITIAL H FIRST-NAME J OHN ENTIRE-NAME Figure 9.5 The STRING Statement (cont.) MOVE SPACES TO ENTIRE-NAME. STRING FIRST-NAME DELIMITED BY SPACE ‘ ‘ DELIMITED BY SIZE MIDDLE-INITIAL DELIMITED BY SPACE ‘ ‘ DELIMITED BY SIZE LAST-NAME DELIMITED BY SPACE INTO ENTIRE-NAME (b) STRING Statement Execution Steps (1) ENTIRE-NAME J OHN (2) ENTIRE-NAME J O H N (b) (3) ENTIRE-NAME J O H N (b) H (4) ENTIRE-NAME J O H N (b) H (b) (5) ENTIRE-NAME J O H N (b) H (b) S M I T H (c) Sequence of transfer Figure 9.6 The UNSTRING Statement 05 NAME-IN-PIECES. 10 LAST-NAME 10 FIRST-NAME 10 MIDDLE-INITIAL PIC X(16). PIC X(10). PIC X. 05 ENTIRE-NAME PIC X(31). (a) Working-Storage Holding Areas MOVE SPACES TO NAME-IN-PIECES. UNSTRING ENTIRE-NAME DELIMITED BY ‘ ‘ INTO FIRST-NAME MIDDLE-INITIAL LAST-NAME. (b) UNSTRING Statement Before Execution LAST-NAME MIDDLE-INITIAL FIRST-NAME ENTIRE-NAME J O H N (b) H (b) S M I T H Figure 9.6 The UNSTRING Statement (1) ENTIRE-NAME J O H N (b) H (b) S M I T H (1) ENTIRE-NAME J O H N (b) H (b) S M I T H J OHN FIRST-NAME H SM I T H MIDDLE-INITIAL LAST-NAME (c) Execution of UNSTRING The WRITE FROM statement FD PRINT-FILE DATA RECORD IS PRINT-LINE. 01 PRINT-LINE PIC X(80). PROCEDURE DIVISION. . . MOVE HEADING-LINE TO PRINT-LINE. WRITE PRINT-LINE AFTER ADVANCING PAGE. (a) Two instruction process FD PRINT-FILE DATA RECORD IS PRINT-LINE. 01 PRINT-LINE PIC X(80). . . 01 HEADING-LINE. 03 PIC X(17) VALUE SPACES. 03 PIC X(25) VALUE ‘ INVENTORY REPORT PREPARED BY GARPL’. PROCEDURE DIVISION. . . WRITE PRINT-LINE FROM HEADING-LINE AFTER ADVANCING PAGE. (b) One instruction process • INITALIZE – 01 GROUP-ITEM • 05 NUMERIC-FIELD PIC 9(4) • 05 ALPHANUMERIC-FIELD PIC X(15) INITIALIZE GROUP-ITEM == MOVE ZEROS TO NUMERIC-FIELD MOVE SPAES TO ALPHANUMERIC-FIELD INITIALIZE GROUP-ITEM REPLACING NUMERIC BY ZERO == MOVE ZEROS TO NUMERIC-FIELD The INITIALIZE statement • Sets elementary or group data names to ‘initial’ value • DOES NOT RESET VALUE TO THAT OF THE VALUE STATEMENT!!! • EX: 01 GROUP-ITEM. 05 NUMERIC-FIELD-1 05 NUMERIC-FIELD-2 05 ALPHANUMERIC-FIELD-1 05 ALPHANUMERIC-FIELD-2 • The statement PIC 9(4). PIC 9(4). PIC X(15). PIC X(20). INITIALIZE GROUP-ITEM • is equivalent to the combined statements: MOVE ZEROS TO NUMERIC-FIELD-1. MOVE ZEROS TO NUMERIC-FIELD-2. MOVE SPACES TO ALPHANUMERIC-FIELD-1. MOVE SPACES TO ALPHANUMERIC-FIELD-2. • ACCEPT • Duplicate Data Names – Qualification – MOVE CORRESPONDING • The Car billing program – Program design – The Completed Program