+ All Categories
Home > Documents > Topic objectives

Topic objectives

Date post: 06-Jan-2016
Category:
Upload: donat
View: 22 times
Download: 0 times
Share this document with a friend
Description:
Topic objectives. By the end of this unit you should - Understand how the PERFORM can be used to transfer control to block of code contained in a paragraph or section.. Know how to use the PERFORM..THRU and the GO TO and understand the restrictions placed on using them. - PowerPoint PPT Presentation
28
1 Topic objectives By the end of this unit you should - Understand how the PERFORM can be used to transfer control to block of code contained in a paragraph or section.. Know how to use the PERFORM..THRU and the GO TO and understand the restrictions placed on using them. Understand the difference between in-line and out-of- line Performs
Transcript
Page 1: Topic objectives

1

Topic objectives

By the end of this unit you should - Understand how the PERFORM can be used to transfer control to block

of code contained in a paragraph or section.. Know how to use the PERFORM..THRU and the GO TO and understand

the restrictions placed on using them. Understand the difference between in-line and out-of-line Performs

Page 2: Topic objectives

2

COBOL Transfer of Control Options

Three keywords used to transfer control in traditional COBOLPERFORMPERFORM

Branches to the first statement in a block of code– Inline

– Or, organized in a labeled paragraph or section somewhere else in the PROCEDURE PROCEDURE DIVISIONDIVISION

At the end of the performed code, control is automatically returned to the next sequential instruction following PERFORMPERFORM

PERFORMPERFORM is a statement with a number of useful options and extensions

GO TOGO TO Unconditional branch to a labeled paragraph or section All statements are executed at that point in time – forward in the program

CALLCALL Invoke another COBOL program Pass parameters with a USINGUSING statement We will discuss CALLCALL in a future section of this course

Of the above three options: PERFORM PERFORM and CALL CALL are best practices for "structured programming" GO TO GO TO is not a best practice, but we will present it, as you will see many

examples of GO TO GO TO in production COBOL, and need to understand it

Page 3: Topic objectives

3

PERFORMPERFORM – External Paragraph or Section – External Paragraph or Section

Structured coding method of branching to – and returning from COBOL paragraphs or sections

With PERFORMPERFORM, the compiler automatically returns control to the "next sequential instruction" after the block of statements in the paragraph or section ends This makes the program's flow of control easy to read,

easy to understand and easy to maintain Less worry about "fall thru" logic

PERFORMPERFORM May be nested:

This is known as a "PERFORMPERFORM chain" – or a series of

PERFORMPERFORM and return branches controlled by the Operating System at run-time.

May NOT be recursive: In this example, within 200-OPEN-FILES200-OPEN-FILES you

may not PERFORM 100-HOUSEKEEPINGPERFORM 100-HOUSEKEEPING Does not depend on physical placement or ordering in

the source files: Although it can help from a read-ability standpoint

to PERFORM PERFORM paragraphs lower (down) in the program listing.

Allows you do "divide and conquer" the design and development of large complex applications.

Do not scope external paragraph PERFORM PERFORM with END-PERFORMEND-PERFORM

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPING.PERFORM 100-HOUSEKEEPING.

PERFORM 300-MAINLINE-RTN.PERFORM 300-MAINLINE-RTN.

PERFORM 500-CLEANUP.PERFORM 500-CLEANUP.

GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

PERFORM 150-INITIALIZE-FIELDS.PERFORM 150-INITIALIZE-FIELDS.

PERFORM 200-OPEN-FILES.PERFORM 200-OPEN-FILES.

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

150-INITIALIZE-FIELDS.150-INITIALIZE-FIELDS.

……

200-OPEN-FILES.200-OPEN-FILES.

……

300-MAINLINE-RTN.300-MAINLINE-RTN.

PERFORM 400-PROCESS-RECORD.PERFORM 400-PROCESS-RECORD.

PERFORM 700-WRITE-RTN.PERFORM 700-WRITE-RTN.

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

400-PROCESS-RECORD.400-PROCESS-RECORD.

… …

500-CLEANUP.500-CLEANUP.

PERFORM 900-CLOSE-FILES.PERFORM 900-CLOSE-FILES.

……

700-WRITE-RTN.700-WRITE-RTN.

……

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPING.PERFORM 100-HOUSEKEEPING.

PERFORM 300-MAINLINE-RTN.PERFORM 300-MAINLINE-RTN.

PERFORM 500-CLEANUP.PERFORM 500-CLEANUP.

GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

PERFORM 150-INITIALIZE-FIELDS.PERFORM 150-INITIALIZE-FIELDS.

PERFORM 200-OPEN-FILES.PERFORM 200-OPEN-FILES.

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

150-INITIALIZE-FIELDS.150-INITIALIZE-FIELDS.

……

200-OPEN-FILES.200-OPEN-FILES.

……

300-MAINLINE-RTN.300-MAINLINE-RTN.

PERFORM 400-PROCESS-RECORD.PERFORM 400-PROCESS-RECORD.

PERFORM 700-WRITE-RTN.PERFORM 700-WRITE-RTN.

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

400-PROCESS-RECORD.400-PROCESS-RECORD.

… …

500-CLEANUP.500-CLEANUP.

PERFORM 900-CLOSE-FILES.PERFORM 900-CLOSE-FILES.

……

700-WRITE-RTN.700-WRITE-RTN.

……

Page 4: Topic objectives

4

PERFORM THRUPERFORM THRU

One variation on PERFORMPERFORM is PERFORM … THRU PERFORM … THRU

PERFORM … THRU PERFORM … THRU allows you to explicitly mark & bound the end of the PERFORMPERFORM chain with a labeled paragraph

All of the procedural statements between: PERFORMPERFORM <paragraphName> and THRU THRU <paragraphName>

…are executed

The best practice is for the exit paragraph to have one COBOL reserved word in it: EXITEXIT This returns control to the next sequential

instruction in the perform chain

Technically, you could PERFORMPERFORM a COBOL paragraph THRUTHRU any other paragraph. However, this often leads to complex and unstructured code Difficult to understand and maintain

So the convention is to PERFORM THRUPERFORM THRU a single paragraph EXITEXIT (as shown)

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPING PERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXIT.THRU 300-EXIT.

PERFORM 500-CLEANUPPERFORM 500-CLEANUP

THRU 500-EXIT.THRU 500-EXIT.

GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

PERFORM 150-INITIALIZE-FIELDSPERFORM 150-INITIALIZE-FIELDS

THRU 150-EXIT.THRU 150-EXIT.

PERFORM 200-OPEN-FILES PERFORM 200-OPEN-FILES

THRU 200-EXIT.THRU 200-EXIT.

PERFORM 800-READ-RTN PERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

100-EXIT.100-EXIT.

EXIT.EXIT.

150-INITIALIZE-FIELDS.150-INITIALIZE-FIELDS.

……

150-EXIT.150-EXIT.

EXIT.EXIT.

200-OPEN-FILES.200-OPEN-FILES.

……

200-EXIT.200-EXIT.

EXIT.EXIT.

……

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPING PERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXIT.THRU 300-EXIT.

PERFORM 500-CLEANUPPERFORM 500-CLEANUP

THRU 500-EXIT.THRU 500-EXIT.

GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

PERFORM 150-INITIALIZE-FIELDSPERFORM 150-INITIALIZE-FIELDS

THRU 150-EXIT.THRU 150-EXIT.

PERFORM 200-OPEN-FILES PERFORM 200-OPEN-FILES

THRU 200-EXIT.THRU 200-EXIT.

PERFORM 800-READ-RTN PERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

100-EXIT.100-EXIT.

EXIT.EXIT.

150-INITIALIZE-FIELDS.150-INITIALIZE-FIELDS.

……

150-EXIT.150-EXIT.

EXIT.EXIT.

200-OPEN-FILES.200-OPEN-FILES.

……

200-EXIT.200-EXIT.

EXIT.EXIT.

…… See Slide NotesSee Slide Notes

Page 5: Topic objectives

5

Inline Inline PERFORMPERFORM

Another variation on PERFORMPERFORM is what's known as an "inline perform"

An inline PERFORMPERFORM allows you to encase COBOL statements and business logic within a structured statement block – which you can group or loop through (looping is the next topic, but Inline PERFORMPERFORM is often used to control loops.

PERFORMPERFORM <statement>

<statement>

END-PERFORM.END-PERFORM.

An in-line PERFORMPERFORM must be delimited by the END-PERFORMEND-PERFORM phrase.

PROCEDURE DIVISION.PROCEDURE DIVISION. PERFORM UNTIL END-OF-FILE PERFORM UNTIL END-OF-FILE

IF NOT END-OF-FILE IF NOT END-OF-FILE

PERFORM 800-READ-INPUT-FILEPERFORM 800-READ-INPUT-FILE

IF NOT END-OF-FILEIF NOT END-OF-FILE

MOVE INPUT-REC TO OUTPUT-RECMOVE INPUT-REC TO OUTPUT-REC

PERFORM 900-WRITE-RECORDPERFORM 900-WRITE-RECORD

ELSEELSE

MOVE HIGH-VALUES MOVE HIGH-VALUES

TO INPUT-RECTO INPUT-REC

PERFORM 1000-CLOSE-FILESPERFORM 1000-CLOSE-FILES

DISPLAY 'NORMAL EOJDISPLAY 'NORMAL EOJ

END-IFEND-IF

END-IFEND-IF

END-PERFORM.END-PERFORM.

……

PERFORM VARYING IDX PERFORM VARYING IDX

FROM 1 BY 1 UNTIL IDX > 100FROM 1 BY 1 UNTIL IDX > 100

MOVE REC-IN MOVE REC-IN

TO REC-TABLE(IDX)TO REC-TABLE(IDX)

END-PERFORM.END-PERFORM.

……

PROCEDURE DIVISION.PROCEDURE DIVISION. PERFORM UNTIL END-OF-FILE PERFORM UNTIL END-OF-FILE

IF NOT END-OF-FILE IF NOT END-OF-FILE

PERFORM 800-READ-INPUT-FILEPERFORM 800-READ-INPUT-FILE

IF NOT END-OF-FILEIF NOT END-OF-FILE

MOVE INPUT-REC TO OUTPUT-RECMOVE INPUT-REC TO OUTPUT-REC

PERFORM 900-WRITE-RECORDPERFORM 900-WRITE-RECORD

ELSEELSE

MOVE HIGH-VALUES MOVE HIGH-VALUES

TO INPUT-RECTO INPUT-REC

PERFORM 1000-CLOSE-FILESPERFORM 1000-CLOSE-FILES

DISPLAY 'NORMAL EOJDISPLAY 'NORMAL EOJ

END-IFEND-IF

END-IFEND-IF

END-PERFORM.END-PERFORM.

……

PERFORM VARYING IDX PERFORM VARYING IDX

FROM 1 BY 1 UNTIL IDX > 100FROM 1 BY 1 UNTIL IDX > 100

MOVE REC-IN MOVE REC-IN

TO REC-TABLE(IDX)TO REC-TABLE(IDX)

END-PERFORM.END-PERFORM.

……

Page 6: Topic objectives

6

Scope Terminators and Paragraph Names

You have seen that many of the COBOL statements can have Scope Terminators: END-IF END-READ END-COMPUTE …

This is actually a coding best practice

However, the last statement before the paragraph (or "exit paragraph") must end in a period.

100-HOUSEKEEPING.100-HOUSEKEEPING.

… …

IF ITEM-2 = "AABBCC" OR "AACCDD" IF ITEM-2 = "AABBCC" OR "AACCDD"

OR "AADDEE"OR "AADDEE"

MOVE ITEM-2 TO ITEM-1MOVE ITEM-2 TO ITEM-1

ELSE ELSE

MOVE ITEM-3 TO ITEM-1MOVE ITEM-3 TO ITEM-1

END-IFEND-IF

100-EXIT.100-EXIT.

EXIT.EXIT. This will not compile!This will not compile!

100-HOUSEKEEPING.100-HOUSEKEEPING.

… …

IF ITEM-2 = "AABBCC" OR "AACCDD" IF ITEM-2 = "AABBCC" OR "AACCDD"

OR "AADDEE"OR "AADDEE"

MOVE ITEM-2 TO ITEM-1MOVE ITEM-2 TO ITEM-1

ELSE ELSE

MOVE ITEM-3 TO ITEM-1MOVE ITEM-3 TO ITEM-1

END-IF.END-IF.

100-EXIT.100-EXIT.

EXIT.EXIT.

Type a period after END-IFType a period after END-IF

Page 7: Topic objectives

7

GO TOGO TO – Unconditional Transfer of Control – Unconditional Transfer of Control

GO TOGO TO branches to the paragraph or section label after the statement. With no automatic, compiler-managed return to

the next sequential instruction Ergo all subsequent statements "fall through"

This can create what is termed "spaghetti code" – which is typically

Difficult to read Very difficult to maintain and modify

The use of GO TOGO TO is universally denounced in the academic computing world And we agree – except for under one very key

and common design pattern (combining GO TOGO TO with PERFORM … THRUPERFORM … THRU)

Our "Best Practices" advice: Do not use GO TOGO TO in COBOL coding, for

transferring control EXCEPT under one conditionEXCEPT under one condition – when you are

using GO TOGO TO - for branching to the exit paragraph in a PERFORM … THRUPERFORM … THRU

This honors the Perform Chain and execution will not "fall through"

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPING PERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXIT.THRU 300-EXIT.

GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

PERFORM 200-OPEN-FILES PERFORM 200-OPEN-FILES

THRU 200-EXIT.THRU 200-EXIT.

PERFORM 800-READ-RTN PERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

IF END-OF-FILEIF END-OF-FILE

DISPLAY "END-OF-JOB"DISPLAY "END-OF-JOB"

PERFORM 900-CLOSE FILESPERFORM 900-CLOSE FILES

THRU 900-EXITTHRU 900-EXIT

GO TO 100-EXITGO TO 100-EXIT ELSEELSE

ADD +1 TO REC-KTRADD +1 TO REC-KTR

MOVE ZEROS TO AMOUNT-TOTMOVE ZEROS TO AMOUNT-TOT

END-IFEND-IF

Perform 300-INIT-FIELDS.Perform 300-INIT-FIELDS.

100-EXIT.100-EXIT.

EXIT.EXIT.

……

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPING PERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXIT.THRU 300-EXIT.

GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

PERFORM 200-OPEN-FILES PERFORM 200-OPEN-FILES

THRU 200-EXIT.THRU 200-EXIT.

PERFORM 800-READ-RTN PERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

IF END-OF-FILEIF END-OF-FILE

DISPLAY "END-OF-JOB"DISPLAY "END-OF-JOB"

PERFORM 900-CLOSE FILESPERFORM 900-CLOSE FILES

THRU 900-EXITTHRU 900-EXIT

GO TO 100-EXITGO TO 100-EXIT ELSEELSE

ADD +1 TO REC-KTRADD +1 TO REC-KTR

MOVE ZEROS TO AMOUNT-TOTMOVE ZEROS TO AMOUNT-TOT

END-IFEND-IF

Perform 300-INIT-FIELDS.Perform 300-INIT-FIELDS.

100-EXIT.100-EXIT.

EXIT.EXIT.

……

Page 8: Topic objectives

8

Transfer of Control Best PracticesTransfer of Control Best Practices

We will cover structured COBOL programming and logic patterns and design in one of the upcoming units, but for now, consider the following:

Structure your program as a chain of PERFORMPERFORM THRUTHRU paragraphs

Within the paragraphs code anything you need to satisfy your business logic requirements, including: CALLCALLs to external programs – CALLCALL is covered in an upcoming unit Nested PERFORMPERFORM Inline PERFORMPERFORM Sequence and conditional logic GO TOGO TO – BUT ONLY GO TOGO TO a PERFORM THRUPERFORM THRU paragraph EXITEXIT

Try not to use COBOL SECTIONSECTIONs Within COBOL SECTIONSECTIONs – COBOL paragraphs are considered at the level of

statements – blocks of code where fall throughfall through will occur The only time you will need a COBOL SECTIONSECTION is when you're programming is invoking

the COBOL SORTSORT verb (which is tied to INPUT and OUTPUT SORTSORT SECTIONSECTIONs)

Page 9: Topic objectives

9

Topic objectives

By the end of this unit you should - Be able to use the PERFORM..TIMES. Understand how the PERFORM..UNTIL works and be able to use it to

implement while or do/repeat loops. Be able to use PERFORM..VARYING to implement counting iteration

such as that implemented in other languages by the for construct.

Page 10: Topic objectives

10

COBOL Looping Options

There are three COBOL programming ways to loop: GO TO <paragraphName>GO TO <paragraphName> combined with an IFIF condition that ends the loop:

Unstructured – creates complex, un-maintainable code NOT a best practice – hence will not be covered

PERFORM <paragraphName> n TIMESPERFORM <paragraphName> n TIMES Structured and simple to understand But only applicable in cases where the number of loop iterations is known at design time

PERFORM <paragraphName> UNTILPERFORM <paragraphName> UNTIL a condition is met Structured ("Good") Can be used when the number of loop iterations is variable or known

Net: Use PERFORM <paragraphName> UNTILPERFORM <paragraphName> UNTIL for your COBOL looping requirements.

When do you need to loop in your COBOL programs? Reading/Writing a file until no more records Looping through rows returned from a database Loading a COBOL internal table Processing a COBOL table sequentially Calculations Algorithms

…in general – you will write few programs that don't loop

Page 11: Topic objectives

11

PERFORMPERFORM <paragraphName> <paragraphName> n n TIMESTIMES Can PERFORM a paragraph a specified

number of times, in a loop controlled by a: Numeric literal Numeric integer variable

Examples: Performs all of the COBOL statements between 300-MAINLINE-RTN300-MAINLINE-RTN and 300-EXIT 300-EXIT 12 12 TIMESTIMES

Performs all of the COBOL statements between 310-SUBTOTALS 310-SUBTOTALS and 310-EXIT 310-EXIT the number of times represented by the integer value in NBR-REPSNBR-REPS

Use PERFORM … n TIMES PERFORM … n TIMES – when you know during development how many loop iterations should be performed at run-time.

WORKKING-STORAGE SECTION.WORKKING-STORAGE SECTION.

77 NBR-REPS S9(4) COMP.77 NBR-REPS S9(4) COMP.

PROCEDURE DIVISION.PROCEDURE DIVISION.

… …

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXITTHRU 300-EXIT

12 TIMES.12 TIMES.

GOBACK.GOBACK.

300-MAINLINE-RTN.300-MAINLINE-RTN.

… …

MOVE IN-REC-KTR TO NBR-REPS.MOVE IN-REC-KTR TO NBR-REPS.

PERFORM 310-SUBTOTALSPERFORM 310-SUBTOTALS

THRU 310-EXITTHRU 310-EXIT

NBR-REPS TIMES.NBR-REPS TIMES.

… …300-EXIT.300-EXIT.

EXIT.EXIT.

WORKKING-STORAGE SECTION.WORKKING-STORAGE SECTION.

77 NBR-REPS S9(4) COMP.77 NBR-REPS S9(4) COMP.

PROCEDURE DIVISION.PROCEDURE DIVISION.

… …

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXITTHRU 300-EXIT

12 TIMES.12 TIMES.

GOBACK.GOBACK.

300-MAINLINE-RTN.300-MAINLINE-RTN.

… …

MOVE IN-REC-KTR TO NBR-REPS.MOVE IN-REC-KTR TO NBR-REPS.

PERFORM 310-SUBTOTALSPERFORM 310-SUBTOTALS

THRU 310-EXITTHRU 310-EXIT

NBR-REPS TIMES.NBR-REPS TIMES.

… …300-EXIT.300-EXIT.

EXIT.EXIT.

Page 12: Topic objectives

12

PERFORMPERFORM UNTIL UNTIL <condition><condition>

Structured method of looping when you only know at run-time how many times the loop should be iterated over

UNTILUNTIL Tests a condition for TRUE/FALSE

If NOT TRUE (repeat – if the condition is FALSE) PERFORMPERFORM the specified Paragraph

If TRUE

– End the loop

– Return program control to the next sequential instruction following the PERFORM UNTILPERFORM UNTIL statement

Additional notes: If the UNTIL condition never becomes true?

Infinite loop – If the program is batch, the TIME= parameter on the

JCL will most likely cancel it– If the program is an online transaction, the operator

will cancel it– Either way, this is not a good thing

There are actually two additional options for PERFORM UNTILPERFORM UNTIL (next slide)

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPINGPERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXITTHRU 300-EXIT

UNTIL NO-MORE-RECORDS.UNTIL NO-MORE-RECORDS. GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

OPEN INPUT IN-FILE.OPEN INPUT IN-FILE.

PERFORM 800-READ-RTNPERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

100-EXIT.100-EXIT.

EXIT.EXIT.

300-MAINLINE-RTN.300-MAINLINE-RTN.

… …

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

300-EXIT.300-EXIT.

EXIT.EXIT.

800-READ-RTN.800-READ-RTN.

READ IN-FILE INTO WS-RECORDREAD IN-FILE INTO WS-RECORD

AT END MOVE 'Y' AT END MOVE 'Y'

TO SW-NO-MORE-RECORDS.TO SW-NO-MORE-RECORDS.

800-EXIT.800-EXIT.

EXIT.EXIT.

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPINGPERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXITTHRU 300-EXIT

UNTIL NO-MORE-RECORDS.UNTIL NO-MORE-RECORDS. GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

OPEN INPUT IN-FILE.OPEN INPUT IN-FILE.

PERFORM 800-READ-RTNPERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

100-EXIT.100-EXIT.

EXIT.EXIT.

300-MAINLINE-RTN.300-MAINLINE-RTN.

… …

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

300-EXIT.300-EXIT.

EXIT.EXIT.

800-READ-RTN.800-READ-RTN.

READ IN-FILE INTO WS-RECORDREAD IN-FILE INTO WS-RECORD

AT END MOVE 'Y' AT END MOVE 'Y'

TO SW-NO-MORE-RECORDS.TO SW-NO-MORE-RECORDS.

800-EXIT.800-EXIT.

EXIT.EXIT.

Page 13: Topic objectives

13

PERFORMPERFORM UNTIL – With TEST UNTIL – With TEST BEFORE or AFTERBEFORE or AFTER PERFORM …UNTILPERFORM …UNTIL may be modified by

one of two clauses, coded before UNTILUNTIL and after the paragraph name:

1.1.WITH TEST BEFOREWITH TEST BEFORE The UNTILUNTIL condition is tested before condition is tested before

each each PERFORMPERFORM execution.  execution.  The Paragraphs will be executed The Paragraphs will be executed 00 or or

more timesmore times Is the default – if this clause is left Is the default – if this clause is left

unspecifiedunspecified

2.2.WITH TEST AFTERWITH TEST AFTER The UNTILUNTIL condition is tested after each condition is tested after each

PERFORMPERFORM execution.  execution.  The Paragraphs will be executed The Paragraphs will be executed 11 or or

more timesmore times

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPINGPERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXIT THRU 300-EXIT

WITH TEST BEFOREWITH TEST BEFORE UNTIL NO-MORE-RECORDS.UNTIL NO-MORE-RECORDS. GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

OPEN INPUT IN-FILE.OPEN INPUT IN-FILE.

PERFORM 800-READ-RTNPERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

100-EXIT.100-EXIT.

EXIT.EXIT.

300-MAINLINE-RTN.300-MAINLINE-RTN.

… …

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

300-EXIT.300-EXIT.

EXIT.EXIT.

800-READ-RTN.800-READ-RTN.

READ IN-FILE INTO WS-RECORDREAD IN-FILE INTO WS-RECORD

……

800-EXIT.800-EXIT.

EXIT.EXIT.

PROCEDURE DIVISION.PROCEDURE DIVISION.

PERFORM 100-HOUSEKEEPINGPERFORM 100-HOUSEKEEPING

THRU 100-EXIT.THRU 100-EXIT.

PERFORM 300-MAINLINE-RTNPERFORM 300-MAINLINE-RTN

THRU 300-EXIT THRU 300-EXIT

WITH TEST BEFOREWITH TEST BEFORE UNTIL NO-MORE-RECORDS.UNTIL NO-MORE-RECORDS. GOBACK.GOBACK.

100-HOUSEKEEPING.100-HOUSEKEEPING.

OPEN INPUT IN-FILE.OPEN INPUT IN-FILE.

PERFORM 800-READ-RTNPERFORM 800-READ-RTN

THRU 800-EXIT.THRU 800-EXIT.

100-EXIT.100-EXIT.

EXIT.EXIT.

300-MAINLINE-RTN.300-MAINLINE-RTN.

… …

PERFORM 800-READ-RTN.PERFORM 800-READ-RTN.

300-EXIT.300-EXIT.

EXIT.EXIT.

800-READ-RTN.800-READ-RTN.

READ IN-FILE INTO WS-RECORDREAD IN-FILE INTO WS-RECORD

……

800-EXIT.800-EXIT.

EXIT.EXIT.

Page 14: Topic objectives

14

Topic objectives

This topic covers basic (simple) sequential file processing – patterns: Open files Read an initial record from the input file Process all records until end-of-input-file

Edit and validate data Compute totals, subtotals and accumulators Write output records Read the next input file record

Close files and end the job In subsequent topics of this course, we will dive much more deeply into

file handling, as a majority of COBOL batch processing depends on concepts and coding patterns that are variations of the above

We will also touch on the basic batch program design patterns you will want to use going forward in this course and later in your production COBOL work

By the end of this chapter you will be able to: OPEN, READ, WRITE and CLOSE files Loop through and input file, processing all of the records until completed

Page 15: Topic objectives

15

Sequential File ProcessingSequential File Processing

Sequential File Processing consists of a well-documented set of processing – Sequential File Processing consists of a well-documented set of processing – or a pattern that you will see in many program requirementsor a pattern that you will see in many program requirements You read one or more input files until:You read one or more input files until:

Your business logic requirements are fulfilledYour business logic requirements are fulfilled End-of-FileEnd-of-File

While reading files you typically:While reading files you typically: Edit or evaluate dataEdit or evaluate data Add numeric values to total and sub-total fields – perform business calculationsAdd numeric values to total and sub-total fields – perform business calculations Assign (MOVE) valuesAssign (MOVE) values WRITE output record(s) – either to an output file, report or bothWRITE output record(s) – either to an output file, report or both

You must be cognizant of:You must be cognizant of: Bad or invalid data (and know what to do about it when it shows up in your fields)Bad or invalid data (and know what to do about it when it shows up in your fields) Empty input filesEmpty input files READ/WRITE I/O errors that may occurREAD/WRITE I/O errors that may occur

After reaching end-of-file you will typically:After reaching end-of-file you will typically: WRITE a final output record, with summary computed values WRITE a final output record, with summary computed values CLOSE all filesCLOSE all files DISPLAY a successful end-of-job messageDISPLAY a successful end-of-job message

Input FileInput FileNote - more Note - more than likely than likely NOTNOT a tape a tape

Device Device

COBOL ProgramCOBOL ProgramBusiness LogicBusiness Logic

Output FileOutput File

Output ReportOutput Report

Record

Bu

ffer

Record

Bu

ffer

Record

Bu

ffer

Record

Bu

ffer

Page 16: Topic objectives

16

Sequential File Processing Pattern – Simple Batch Design PatternSequential File Processing Pattern – Simple Batch Design Pattern

This first batch application pattern "Process One Input File" – consists of This first batch application pattern "Process One Input File" – consists of the following pattern the following pattern

Perform an initialization routinePerform an initialization routine Initialize values in Working-StorageInitialize values in Working-Storage OPEN the files – for either input or outputOPEN the files – for either input or output PERFORM a "priming" input-record read – an initial read statement that PERFORM a "priming" input-record read – an initial read statement that

simplifies:simplifies: Empty input-file-processing problemsEmpty input-file-processing problems Reading past end-of-file logic problemsReading past end-of-file logic problems

Perform a process-the-file routine, until end-of-input-filePerform a process-the-file routine, until end-of-input-file Validate the data Validate the data – using the conditional logic statements learned in this unit Move the data Move the data – using the assignment statements learned in this unit Do computations, calculations, etc. Do computations, calculations, etc. – using the COBOL math statements Write the recordWrite the record Read the next input recordRead the next input record

Perform an end-of-job routinePerform an end-of-job routine Complete final computationsComplete final computations WRITE final output record with final computationsWRITE final output record with final computations CLOSE all files and display an end-of-job message on the ConsoleCLOSE all files and display an end-of-job message on the Console

Page 17: Topic objectives

17

File I/O Review – OPENOPEN

…ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.

SELECT INTFILENAME ASSIGN TO … …DATA DIVISION.FILE SECTIONFD INTFILENAME…01 OUT-REC.

…PROCEDURE DIVISION.

OPEN OPEN

INPUT FILE1, FILE2INPUT FILE1, FILE2

OUTPUT FILE3, FILE4.OUTPUT FILE3, FILE4.…

ExternalExternalDeviceDevice

External FileExternal File

Recall the following:Recall the following: SELECT/ASSIGNSELECT/ASSIGN connects your

internal (logical) filename with external (physical) file-spec

FILE SECTIONFILE SECTION is required for each SELECT/ASSIGNSELECT/ASSIGN file

OPENOPEN references the logical (internal) filename

Can OPENOPEN multiple files with one statement (as shown)

Note carefully the syntax for OPEN See Slide Notes, for See Slide Notes, for additional learning contentadditional learning content

Record BufferRecord Buffer

Page 18: Topic objectives

18

File I/O Review – READ INTOREAD INTO

ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION.

SELECT INTFILENAME ASSIGN TO … DATA DIVISION. FILE SECTION FD INTFILENAME … 01 IN-REC. WORKING-STORAGE SECTION 01 IN-REC-WS. 01 END-OF-FILE-FLAG PIC X. PROCEDURE DIVISION.

OPEN INPUT INTFILENAME.

READ INTFILENAME INTO IN-RECREAD INTFILENAME INTO IN-REC

AT END MOVE ‘Y’ to END-OF-FILE-FLAG.AT END MOVE ‘Y’ to END-OF-FILE-FLAG.…

ExternalExternalDeviceDevice

External FileExternal File

See Slide Notes, See Slide Notes, for additional for additional learning contentlearning content

FILE

READ

Recall the following:Recall the following: File must be OPENOPEN before you try to READREAD from it READREAD retrieves each record from an external file:

- Into the record specified in your FILE SECTIONFILE SECTION

* Note that the DATA DIVISION's FILE SECTION is sometimes referred to as an I/O "buffer"* Note that the DATA DIVISION's FILE SECTION is sometimes referred to as an I/O "buffer"

- Into WORKING-STORAGEWORKING-STORAGE - if you've coded READ INTOREAD INTO AT ENDAT END condition occurs after the last record is READREAD from the file

- If you attempt to read past end-of-file your program will ABEND

Record BufferRecord Buffer

Page 19: Topic objectives

19

File I/O Review – WRITE FROMWRITE FROM

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.

SELECT OUTFILENAME ASSIGN TO …DATA DIVISION.FILE SECTIONFD OUTFILENAME…01 OUT-REC. WORKING-STORAGE SECTION 01 OUT-REC-WS.PROCEDURE DIVISION.

OPEN OUTPUT OUTFILENAME.

WRITE OUT-REC WRITE OUT-REC

FROM OUT-REC-WS.FROM OUT-REC-WS.

……

ExternalExternalDeviceDevice

External FileExternal File

See Slide Notes, See Slide Notes, for additional for additional learning contentlearning content

Recall the following:Recall the following: File must be OPENOPEN before you try to WRITE WRITE a record to it

- An ABEND condition occurs, if try to write to an un-opened file WRITEWRITE creates a new record at the end of a sequential/external file:

- From the record specified in your FILE SECTIONFILE SECTION

- From WORKING-STORAGEWORKING-STORAGE - if you've coded WRITE FROMWRITE FROM CloseClose opened output files before your program ends

WRITE

OPERATION

Record BufferRecord Buffer

Page 20: Topic objectives

20

File I/O Review – CLOSECLOSE

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.

SELECT INTFILENAME ASSIGN TO ……DATA DIVISION.FILE SECTIONFD INTFILENAME…01 OUT-REC.

…PROCEDURE DIVISION.

CLOSE CLOSE

FILE1, FILE2,FILE1, FILE2,

FILE3, FILE4.FILE3, FILE4.…

ExternalExternalDeviceDevice

External FileExternal File

Recall the following:Recall the following: CLOSE CLOSE every opened file

- Input file

- Output file Can CLOSECLOSE multiple files with one

statement (as shown) Can re-OPEN a file after closing

Note carefully the syntax for CLOSE

FILE

CLOSE

See Slide Notes, for additional learning content

Record BufferRecord Buffer

Page 21: Topic objectives

21

Sequential File Processing Pattern – Pseudo-Code

In a subsequent unit of this course we will cover the concept of design and programming patterns with COBOL

For now, here is your first simple, sequential file-handling pattern, in pseudo-code.

Study the control-flow to understand the paragraph PERFORM process, and purpose of each statement block

Tie this back to the code you the previous slide in this unit.

What is not part of this processing pattern are the actual details of the: Data Validation Calculations and computations… which can be extremely complex and will most

likely take up the majority percentage of your coding cycles.

There are other algorithms that can do the same sequential process, but we feel this is a simple, easy-to-read/maintain/debug-support/and extend approach

PROCEDURE DIVISION.PROCEDURE DIVISION.

Do Init-RoutineDo Init-Routine

Do Process-FilesDo Process-Files

until No-More-Datauntil No-More-Data

Do End-of-Job-RoutineDo End-of-Job-Routine

GOBACK.GOBACK.

Init-Routine.Init-Routine.

Open filesOpen files

READ Input-FileREAD Input-File

Process-Files.Process-Files.

Validate input dataValidate input data

Perform calculations and Perform calculations and

sub-totalssub-totals

Move data to output RecordMove data to output Record

Write Output-RecordWrite Output-Record

READ Input-FileREAD Input-File

End-of-Job-Routine.End-of-Job-Routine.

Final computationsFinal computations

Move Data to output RecordMove Data to output Record

Write output recordWrite output record

Close FilesClose Files

PROCEDURE DIVISION.PROCEDURE DIVISION.

Do Init-RoutineDo Init-Routine

Do Process-FilesDo Process-Files

until No-More-Datauntil No-More-Data

Do End-of-Job-RoutineDo End-of-Job-Routine

GOBACK.GOBACK.

Init-Routine.Init-Routine.

Open filesOpen files

READ Input-FileREAD Input-File

Process-Files.Process-Files.

Validate input dataValidate input data

Perform calculations and Perform calculations and

sub-totalssub-totals

Move data to output RecordMove data to output Record

Write Output-RecordWrite Output-Record

READ Input-FileREAD Input-File

End-of-Job-Routine.End-of-Job-Routine.

Final computationsFinal computations

Move Data to output RecordMove Data to output Record

Write output recordWrite output record

Close FilesClose Files

Page 22: Topic objectives

22

Putting it All Together - A Complete Example of a Sequential File Processing Program Read through (study) the code in this sample

small sequential file processing program. Trace the concepts, COBOL syntax and

statement semantics learned in this, and the previous Unit to the actual code in this program.

Feel free to refer-back to your slides, the course supplemental text or your notes – if necessary

Select External FileSelect External FileAssign to COBOL Assign to COBOL (internal) file name(internal) file name

FILE SECTION FDsFILE SECTION FDs

Matching the SELECT/ASSIGNMatching the SELECT/ASSIGNClauses for each external fileClauses for each external file

Note – this example continues over the next three slidesNote – this example continues over the next three slides

Page 23: Topic objectives

23

A Complete Example of a Sequential File Processing Program – WORKING-STORAGE SECTION

File Status fieldsFile Status fieldsAutomatically updatedAutomatically updatedBy COBOL and the O.S.By COBOL and the O.S.

Input and Output recordsInput and Output records

You will often see these You will often see these coded in WORKING-coded in WORKING-STORAGE because they STORAGE because they are easier to find in a are easier to find in a System log file (called a System log file (called a DUMP file) if your DUMP file) if your program files (ABENDS) program files (ABENDS) when executing on the when executing on the mainframemainframe

Holding fields for intermediateHolding fields for intermediateresult calculations and valuesresult calculations and values

A final-totals output recordA final-totals output record

Page 24: Topic objectives

24

A Complete Example of a Sequential File Processing Program – PROCEDURE DIVISION

Our Sequential File Pattern – in COBOLOur Sequential File Pattern – in COBOL

Read the code line-by-line (as if you were debugging it –Read the code line-by-line (as if you were debugging it –In other words, if there's a PERFORM – jump down to theIn other words, if there's a PERFORM – jump down to thePerformed paragraph. When finished jump back, etc.)Performed paragraph. When finished jump back, etc.)

Note the use of:Note the use of:• PERFORM THRU PERFORM THRU • AT END/GO TO "EXIT" AT END/GO TO "EXIT" • PERFORM UNTILPERFORM UNTIL

• For each record in the input file, do 100-MAINLINEFor each record in the input file, do 100-MAINLINE

EEDDIITTSS

ProcessingProcessing• CalculationsCalculations• ComputationsComputations

MMOOVVEE

Write the current recordWrite the current record

Read the next record in the fileRead the next record in the file

Page 25: Topic objectives

25

A Complete Example of a Sequential File Processing Program – No More Records in the File When there are no more When there are no more

records left to processrecords left to process Do final calculationsDo final calculations Move the fields to final output recordMove the fields to final output record WRITEWRITE CLOSE all filesCLOSE all files Display aDisplay a message on the console message on the console

Page 26: Topic objectives

26

Lab Assignment

From the course workshop documents, do the following labs:1. Process Input File1

2. Process InputFile2

Page 27: Topic objectives

27

Unit

Topics:Topics:

COBOL General Language COBOL General Language RulesRules

Assignment Statements and Internal Data Representation

Math Operations

Conditional Logic

Transfer of control

COBOL Looping Constructs

Reading and Writing Files - Review

Java and .NET EquivalentsJava and .NET Equivalents

Page 28: Topic objectives

28

Java .NET COBOL EquivalentsCOBOLCOBOL JAVAJAVA VB.NETVB.NETMOVE MOVE (elementary field) Java variable assignment (right to left) Variable assignment (right to left)

MOVE MOVE (group field move) Java OBJECT assignment (right to left) Variable assignment (right to left)

MOVE CORRESPONDINGMOVE CORRESPONDING N/A N/A

MOVE with OF qualifierMOVE with OF qualifier Qualified assignment var1.var2.var3 Qualified assignment var1.var2.var3

ADDADD Standard math computation (no separate statement) Standard math computation (no separate statement)

SUBTRACTSUBTRACT "" ""

MULTIPLYMULTIPLY "" ""

DIVIDEDIVIDE "" ""

COMPUTECOMPUTE "" ""

IF/ELSE AND/ORIF/ELSE AND/OR If If

EVALUATE EVALUATE case case

PERFORMPERFORM Method invocation Method invocation

PERFORM THRUPERFORM THRU While or FOR – method invocation While or FOR – method invocation

COBOL Paragraphs and SectionsCOBOL Paragraphs and Sections Java Labels Label

GO TOGO TO Break/Continue can refer to a Label (same behavior as GO TO EXIT) Break/Continue can refer to a Label (same behavior as GO TO EXIT)

PERFORM THRU UNTILPERFORM THRU UNTIL While or FOR – method invocation While or FOR – method invocation

OPENOPEN //comment, as Java opens files automatically OPEN()

CLOSECLOSE DataInputStream.close() CLOSE()

WRITEWRITE DataInputStream/BufferedInputStream/FileInputStream StreamWriter/BinaryWriter

READREAD DataInputStream/BufferedInputStream/FileInputStream StreamReader/BinaryReader

ACCEPTACCEPT Java.util.Scannner (System.in) Console.Read

DISPLAYDISPLAY System.out.println Console.WriteLine – or Console.Write


Recommended