1 / 28

Topic objectives

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.

donat
Télécharger la présentation

Topic objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  2. COBOL Transfer of Control Options • Three keywords used to transfer control in traditional COBOL PERFORM • Branches to the first statement in a block of code • Inline • Or, organized in a labeled paragraph or section somewhere else in the PROCEDURE DIVISION • At the end of the performed code, control is automatically returned to the next sequential instruction following PERFORM • PERFORM is a statement with a number of useful options and extensions GO TO • Unconditional branch to a labeled paragraph or section • All statements are executed at that point in time – forward in the program CALL • Invoke another COBOL program • Pass parameters with a USING statement • We will discuss CALL in a future section of this course • Of the above three options: • PERFORM and CALL are best practices for "structured programming" • GO TO is not a best practice, but we will present it, as you will see many examples of GO TO in production COBOL, and need to understand it

  3. PERFORM – External Paragraph or Section PROCEDURE DIVISION. PERFORM 100-HOUSEKEEPING. PERFORM 300-MAINLINE-RTN. PERFORM 500-CLEANUP. GOBACK. 100-HOUSEKEEPING. PERFORM 150-INITIALIZE-FIELDS. PERFORM 200-OPEN-FILES. PERFORM 800-READ-RTN. 150-INITIALIZE-FIELDS. … 200-OPEN-FILES. … 300-MAINLINE-RTN. PERFORM 400-PROCESS-RECORD. PERFORM 700-WRITE-RTN. PERFORM 800-READ-RTN. 400-PROCESS-RECORD. … 500-CLEANUP. PERFORM 900-CLOSE-FILES. … 700-WRITE-RTN. … • Structured coding method of branching to – and returning from COBOL paragraphs or sections • With PERFORM, 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 • PERFORM • May be nested: • This is known as a "PERFORMchain" – or a series of PERFORMand return branches controlled by the Operating System at run-time. • May NOT be recursive: • In this example, within 200-OPEN-FILES you may notPERFORM 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 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 with END-PERFORM

  4. PROCEDURE DIVISION. PERFORM 100-HOUSEKEEPING THRU 100-EXIT. PERFORM 300-MAINLINE-RTN THRU 300-EXIT. PERFORM 500-CLEANUP THRU 500-EXIT. GOBACK. 100-HOUSEKEEPING. PERFORM 150-INITIALIZE-FIELDS THRU 150-EXIT. PERFORM 200-OPEN-FILES THRU 200-EXIT. PERFORM 800-READ-RTN THRU 800-EXIT. 100-EXIT. EXIT. 150-INITIALIZE-FIELDS. … 150-EXIT. EXIT. 200-OPEN-FILES. … 200-EXIT. EXIT. … PERFORM THRU • One variation on PERFORM is PERFORM … THRU • PERFORM … THRU allows you to explicitly mark & bound the end of the PERFORM chain with a labeled paragraph • All of the procedural statements between: PERFORM <paragraphName> and THRU <paragraphName> …are executed • The best practice is for the exit paragraph to have one COBOL reserved word in it:EXIT • This returns control to the next sequential instruction in the perform chain • Technically, you could PERFORM a COBOL paragraph THRU any other paragraph. However, this often leads to complex and unstructured code • Difficult to understand and maintain  • So the convention is to PERFORM THRU a single paragraph EXIT (as shown)  See Slide Notes

  5. Inline PERFORM PROCEDURE DIVISION. PERFORM UNTIL END-OF-FILE IF NOT END-OF-FILE PERFORM 800-READ-INPUT-FILE IF NOT END-OF-FILE MOVE INPUT-REC TO OUTPUT-REC PERFORM 900-WRITE-RECORD ELSE MOVE HIGH-VALUES TO INPUT-REC PERFORM 1000-CLOSE-FILES DISPLAY 'NORMAL EOJ END-IF END-IF END-PERFORM. … PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > 100 MOVE REC-IN TO REC-TABLE(IDX) END-PERFORM. … • Another variation on PERFORM is what's known as an "inline perform" • An inline PERFORMallows 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 PERFORMis often used to control loops. PERFORM <statement> <statement> … END-PERFORM. • An in-line PERFORM must be delimited by the END-PERFORM phrase.

  6. Scope Terminators and Paragraph Names 100-HOUSEKEEPING. … IF ITEM-2 = "AABBCC" OR "AACCDD" OR "AADDEE" MOVE ITEM-2 TO ITEM-1 ELSE MOVE ITEM-3 TO ITEM-1 END-IF 100-EXIT. EXIT. • 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. This will not compile! Type a period after END-IF 100-HOUSEKEEPING. … IF ITEM-2 = "AABBCC" OR "AACCDD" OR "AADDEE" MOVE ITEM-2 TO ITEM-1 ELSE MOVE ITEM-3 TO ITEM-1 END-IF. 100-EXIT. EXIT.

  7. GO TO – Unconditional Transfer of Control PROCEDURE DIVISION. PERFORM 100-HOUSEKEEPING THRU 100-EXIT. PERFORM 300-MAINLINE-RTN THRU 300-EXIT. GOBACK. 100-HOUSEKEEPING. PERFORM 200-OPEN-FILES THRU 200-EXIT. PERFORM 800-READ-RTN THRU 800-EXIT. IF END-OF-FILE DISPLAY "END-OF-JOB" PERFORM 900-CLOSE FILES THRU 900-EXIT GO TO 100-EXIT ELSE ADD +1 TO REC-KTR MOVE ZEROS TO AMOUNT-TOT END-IF Perform 300-INIT-FIELDS. 100-EXIT. EXIT. … • GO 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 TOis universally denounced in the academic computing world • And we agree – except for under one very key and common design pattern (combining GO TO with PERFORM … THRU)  • Our "Best Practices" advice: • Do not use GO TO in COBOL coding, for transferring control • EXCEPT under one condition – when you are using GO TO - for branching to the exit paragraph in a PERFORM … THRU • This honors the Perform Chain and execution will not "fall through"

  8. Transfer 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 PERFORMTHRU paragraphs • Within the paragraphs code anything you need to satisfy your business logic requirements, including: • CALLs to external programs – CALL is covered in an upcoming unit • Nested PERFORM • Inline PERFORM • Sequence and conditional logic • GO TO – BUT ONLYGO TO a PERFORM THRU paragraph EXIT • Try not to use COBOL SECTIONs • Within COBOL SECTIONs – COBOL paragraphs are considered at the level of statements – blocks of code where fall through will occur • The only time you will need a COBOL SECTION is when you're programming is invoking the COBOL SORT verb (which is tied to INPUT and OUTPUT SORTSECTIONs)

  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.

  10. COBOL Looping Options • There are three COBOL programming ways to loop: • GO TO <paragraphName> combined with an IF condition that ends the loop: • Unstructured – creates complex, un-maintainable code • NOT a best practice – hence will not be covered • PERFORM <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> UNTIL a condition is met • Structured ("Good") • Can be used when the number of loop iterations is variable or known • Net: Use PERFORM <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

  11. WORKKING-STORAGE SECTION. 77 NBR-REPS S9(4) COMP. PROCEDURE DIVISION. … PERFORM 300-MAINLINE-RTN THRU 300-EXIT 12 TIMES. GOBACK. 300-MAINLINE-RTN. … MOVE IN-REC-KTR TO NBR-REPS. PERFORM 310-SUBTOTALS THRU 310-EXIT NBR-REPS TIMES. … 300-EXIT. EXIT. PERFORM <paragraphName> n TIMES • 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-RTNand300-EXIT 12 TIMES • Performs all of the COBOL statements between 310-SUBTOTALS and 310-EXIT the number of times represented by the integer value in NBR-REPS Use PERFORM … n TIMES – when you know during development how many loop iterations should be performed at run-time.

  12. PROCEDURE DIVISION. PERFORM 100-HOUSEKEEPING THRU 100-EXIT. PERFORM 300-MAINLINE-RTN THRU 300-EXIT UNTIL NO-MORE-RECORDS. GOBACK. 100-HOUSEKEEPING. OPEN INPUT IN-FILE. PERFORM 800-READ-RTN THRU 800-EXIT. 100-EXIT. EXIT. 300-MAINLINE-RTN. … PERFORM 800-READ-RTN. 300-EXIT. EXIT. 800-READ-RTN. READ IN-FILE INTO WS-RECORD AT END MOVE 'Y' TO SW-NO-MORE-RECORDS. 800-EXIT. EXIT. PERFORMUNTIL <condition> • Structured method of looping when you only know at run-time how many times the loop should be iterated over • UNTIL • Tests a condition for TRUE/FALSE • If NOT TRUE (repeat – if the condition is FALSE) PERFORM the specified Paragraph • If TRUE • End the loop • Return program control to the next sequential instruction following the PERFORM 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 UNTIL (next slide)

  13. PROCEDURE DIVISION. PERFORM 100-HOUSEKEEPING THRU 100-EXIT. PERFORM 300-MAINLINE-RTN THRU 300-EXIT WITH TEST BEFORE UNTIL NO-MORE-RECORDS. GOBACK. 100-HOUSEKEEPING. OPEN INPUT IN-FILE. PERFORM 800-READ-RTN THRU 800-EXIT. 100-EXIT. EXIT. 300-MAINLINE-RTN. … PERFORM 800-READ-RTN. 300-EXIT. EXIT. 800-READ-RTN. READ IN-FILE INTO WS-RECORD … 800-EXIT. EXIT. PERFORMUNTIL – With TEST BEFORE or AFTER • PERFORM …UNTIL may be modified by one of two clauses, coded before UNTIL and after the paragraph name: • WITH TEST BEFORE • TheUNTIL condition is tested before each PERFORM execution.  • The Paragraphs will be executed 0 or more times • Is the default – if this clause is left unspecified • WITH TEST AFTER • TheUNTIL condition is tested after each PERFORM execution.  • The Paragraphs will be executed 1 or more times

  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

  15. Sequential File Processing • Sequential File Processing consists of a well-documented set of processing – or a pattern that you will see in many program requirements • You read one or more input files until: • Your business logic requirements are fulfilled • End-of-File • While reading files you typically: • Edit or evaluate data • Add numeric values to total and sub-total fields – perform business calculations • Assign (MOVE) values • WRITE output record(s) – either to an output file, report or both • You must be cognizant of: • Bad or invalid data (and know what to do about it when it shows up in your fields) • Empty input files • READ/WRITE I/O errors that may occur • After reaching end-of-file you will typically: • WRITE a final output record, with summary computed values • CLOSE all files • DISPLAY a successful end-of-job message Output Report Input File Note - more than likely NOT a tape Device  COBOL Program Business Logic Output File Record Buffer Record Buffer

  16. Sequential File Processing Pattern – Simple Batch Design Pattern This first batch application pattern "Process One Input File" – consists of the following pattern • Perform an initialization routine • Initialize values in Working-Storage • OPEN the files – for either input or output • PERFORM a "priming" input-record read – an initial read statement that simplifies: • Empty input-file-processing problems • Reading past end-of-file logic problems • Perform a process-the-file routine, until end-of-input-file • Validate the data – using the conditional logic statements learned in this unit • Move the data – using the assignment statements learned in this unit • Do computations, calculations, etc. – using the COBOL math statements • Write the record • Read the next input record • Perform an end-of-job routine • Complete final computations • WRITE final output record with final computations • CLOSE all files and display an end-of-job message on the Console

  17. External Device External File File I/O Review – OPEN … ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT INTFILENAME ASSIGN TO … … DATA DIVISION. FILE SECTION FD INTFILENAME… 01 OUT-REC. … PROCEDURE DIVISION. … OPEN INPUT FILE1, FILE2 OUTPUT FILE3, FILE4. … Record Buffer Recall the following: • SELECT/ASSIGN connects your internal (logical) filename with external (physical) file-spec • FILE SECTION is required for each SELECT/ASSIGN file • OPEN references the logical (internal) filename • Can OPEN multiple files with one statement (as shown) • Note carefully the syntax for OPEN See Slide Notes, for additional learning content

  18. External Device External File File I/O Review – READ 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-REC AT END MOVE ‘Y’ to END-OF-FILE-FLAG. … F I L E R E A D  Record Buffer Recall the following: • File must be OPEN before you try to READ from it • READ retrieves each record from an external file: - Into the record specified in your FILE SECTION * Note that the DATA DIVISION's FILE SECTION is sometimes referred to as an I/O "buffer" - Into WORKING-STORAGE - if you've coded READ INTO • AT END condition occurs after the last record is READ from the file - If you attempt to read past end-of-file your program will ABEND See Slide Notes, for additional learning content

  19. External Device External File File I/O Review – WRITE FROM ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT OUTFILENAME ASSIGN TO … DATA DIVISION. FILE SECTION FD OUTFILENAME… 01 OUT-REC. WORKING-STORAGE SECTION 01 OUT-REC-WS. PROCEDURE DIVISION. OPEN OUTPUT OUTFILENAME. WRITE OUT-REC FROM OUT-REC-WS. … W R I T E O P E R A T I O N Record Buffer Recall the following: • File must be OPEN before you try to WRITE a record to it - An ABEND condition occurs, if try to write to an un-opened file • WRITE creates a new record at the end of a sequential/external file: - From the record specified in your FILE SECTION - From WORKING-STORAGE - if you've coded WRITE FROM • Close opened output files before your program ends See Slide Notes, for additional learning content

  20. External Device External File File I/O Review – CLOSE ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT INTFILENAME ASSIGN TO … … DATA DIVISION. FILE SECTION FD INTFILENAME… 01 OUT-REC. … PROCEDURE DIVISION. … CLOSE FILE1, FILE2, FILE3, FILE4. … F I L E C L O S E Record Buffer Recall the following: • CLOSE every opened file - Input file - Output file • Can CLOSE multiple files with one statement (as shown) • Can re-OPEN a file after closing • Note carefully the syntax for CLOSE See Slide Notes, for additional learning content

  21. PROCEDURE DIVISION. Do Init-Routine Do Process-Files until No-More-Data Do End-of-Job-Routine GOBACK. Init-Routine. Open files READ Input-File Process-Files. Validate input data Perform calculations and sub-totals Move data to output Record Write Output-Record READ Input-File End-of-Job-Routine. Final computations Move Data to output Record Write output record Close Files 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

  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 File Assign to COBOL (internal) file name FILE SECTION FDs Matching the SELECT/ASSIGN Clauses for each external file Note – this example continues over the next three slides

  23. A Complete Example of a Sequential File Processing Program – WORKING-STORAGE SECTION File Status fields Automatically updated By COBOL and the O.S. A final-totals output record Input and Output records You will often see these coded in WORKING-STORAGE because they are easier to find in a System log file (called a DUMP file) if your program files (ABENDS) when executing on the mainframe Holding fields for intermediate result calculations and values

  24. A Complete Example of a Sequential File Processing Program – PROCEDURE DIVISION E D I T S • Processing • Calculations • Computations • Our Sequential File Pattern – in COBOL • Read the code line-by-line (as if you were debugging it – • In other words, if there's a PERFORM – jump down to the • Performed paragraph. When finished jump back, etc.) • Note the use of: • PERFORM THRU • AT END/GO TO "EXIT" • PERFORM UNTIL • For each record in the input file, do 100-MAINLINE M O V E Write the current record Read the next record in the file

  25. A Complete Example of a Sequential File Processing Program – No More Records in the File • When there are no more records left to process • Do final calculations • Move the fields to final output record • WRITE • CLOSE all files • Display a message on the console

  26. Lab Assignment From the course workshop documents, do the following labs: • Process Input File1 • Process InputFile2

  27. COBOL General Language Rules Unit Topics: • Assignment Statements and Internal Data Representation • Math Operations • Conditional Logic • Transfer of control • COBOL Looping Constructs • Reading and Writing Files - Review • Java and .NET Equivalents

  28. Java .NET COBOL Equivalents

More Related