150 likes | 162 Views
Learn about PERFORM, inline PERFORM, conditional branches, STRING, UNSTRING, INSPECT statements in COBOL's Procedure Division. Figure illustrations provide a clear guide on COBOL program structures and processing techniques. Enhance your COBOL programming skills with detailed explanations and examples.
E N D
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 CONDITION? TRUE A CONDITION? FALSE FALSE TRUE (a) DO WHILE Construct (b) DO UNTIL Construct
READ INPUT-FILE AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH.PERFORM PROCESS-RECORDS UNTIL DATA-REMAINS-SWITCH = ‘NO’. PROCESS-RECORDS.. . . 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 . . . END-READEND-PERFORM. (b) False Condition Branch with In-Line Perform Figure 9.2 Structure of a COBOL Program Procedure Division statements to process the current record Procedure Division statements to process the current record
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 PIC 9(9). . . .01 PRINT-LINE. 05 SOC-SEC-NUM-OUT 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
Before Execution LAST-NAME S M I T H MIDDLE-INITIAL H FIRST-NAME J O H N ENTIRE-NAME Figure 9.5 The STRING Statement 05 NAME-IN-PIECES. 10 LAST-NAME PIC X(16). 10 FIRST-NAME PIC X(10). 10 MIDDLE-INITIAL PIC X. 05 ENTIRE-NAME PIC X(29). (a) Working-Storage Holding Areas
(1) ENTIRE-NAME J O H N (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 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 (c) Sequence of transfer
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 05 NAME-IN-PIECES. 10 LAST-NAME PIC X(16). 10 FIRST-NAME PIC X(10). 10 MIDDLE-INITIAL 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
J O H N S M I T H (1) 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 H FIRST-NAME MIDDLE-INITIAL LAST-NAME (c) Execution of UNSTRING
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 The WRITE FROM statement
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 PIC 9(4). 05 NUMERIC-FIELD-2 PIC 9(4). 05 ALPHANUMERIC-FIELD-1 PIC X(15). 05 ALPHANUMERIC-FIELD-2 PIC X(20). • The statement 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