1 / 54

CHAPTER 4 Coding Complete COBOL Programs

CHAPTER 4 Coding Complete COBOL Programs. A Closer Look at the PROCEDURE DIVISION and the PERFORM Statement. A REVIEW OF THE FIRST THREE DIVISIONS. The IDENTIFICATION and ENVIRONMENT DIVISIONs supply information about the nature of the program and the specific equipment and files.

Sophia
Download Presentation

CHAPTER 4 Coding Complete COBOL Programs

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. CHAPTER 4Coding Complete COBOL Programs A Closer Look at the PROCEDURE DIVISION and the PERFORM Statement

  2. A REVIEW OF THE FIRST THREE DIVISIONS • The IDENTIFICATION and ENVIRONMENT DIVISIONs supply information about the nature of the program and the specific equipment and files. • The FILE SECTION of the DATA DIVISION defines the input and output records. Structured COBOL Programming, Stern & Stern, 9th Edition

  3. A REVIEW OF THE FIRST THREE DIVISIONS • The WORKING-STORAGE SECTION of the DATA DIVISION is used for defining any areas not part of input and output files. • The instructions in the PROCEDURE DIVISION read and process the data and produce the output information. Structured COBOL Programming, Stern & Stern, 9th Edition

  4. THE FORMAT OF THE PROCEDURE DIVISION PARAGRAPHS THAT SERVE AS MODULES – Structure Chart documentation serves as the basis for determining paragraphs needed • The PROCEDURE DIVISION is divided into paragraphs. • Each paragraph is an independent module or routine that includes a series of instructions designed to perform a specific set of operations. • Using the structure chart as a guide paragraphs are written from the top down – left to right - ie. 100’s, 200’s, 300’s etc. Structured COBOL Programming, Stern & Stern, 9th Edition

  5. THE FORMAT OF THE PROCEDURE DIVISION • Paragraph-names, like the PROCEDURE DIVISION entry itself, are coded in Area A. • All other entries in the PROCEDURE DIVISION are coded in Area B. • Paragraph-names, like the PROCEDURE DIVISION entry, end with a period. Structured COBOL Programming, Stern & Stern, 9th Edition

  6. THE FORMAT OF THE PROCEDURE DIVISION PARAGRAPHS THAT SERVE AS MODULES • Rules for forming paragraph-names are the same as rules for forming data-names except that a paragraph-name may have all digits. • Paragraph-names must be unique, meaning that two paragraphs may not have the same name. • Paragraph names must be meaningful – ie. Reflect the purpose of the paragraph • Similarly, a data-name cannot also serve as a paragraph-name. Structured COBOL Programming, Stern & Stern, 9th Edition

  7. Paragraphs That Serve as Modules • We will use descriptive paragraph-names along with a numeric prefix such as 200-PROCESS-RTN to identify the type of paragraph. • A paragraph with a prefix of 200- is located after paragraph 100--XXX and before paragraph 300-- YYY. Structured COBOL Programming, Stern & Stern, 9th Edition

  8. Statements within Paragraphs • Each paragraph in a COBOL program consists of statements, where a statement begins with a verb such as READ, MOVE, or WRITE, or a condition such as IF A = B .... • As noted, all COBOL statements are coded in Area B whereas paragraph-names are coded in Area A. • Statements that end with a period are called sentences. • Use periods only • After Paragraph Names • After the last statement in a paragraph Structured COBOL Programming, Stern & Stern, 9th Edition

  9. Statements within Paragraphs • With COBOL 85 only the last statement in a paragraph ends with a period. • With COBOL 74, each statement typically ends with a period. • Recommended that each statement be coded on an individual line. And use of indentation • Makes programs much easier to read and debug. Structured COBOL Programming, Stern & Stern, 9th Edition

  10. THE SEQUENCE OF INSTRUCTIONS IN A PROGRAM • Instructions are typically executed in sequence • Unless a PERFORM statement is encountered. • PERFORM UNTIL ... END-PERFORM • A loop that repeatedly executes • All the statements between PERFORM and END-PERFORM • Until some condition is met • Instruction within the loop must do something to be sure the condition is met • Known as an inline PERFORM • A PERFORM paragraph-name UNTIL .... • Another form of using PERFORM to implement a loop Structured COBOL Programming, Stern & Stern, 9th Edition

  11. PERFORM .... UNTIL • Pretest loop – the condition stated is tested • If condition is not met paragraph / instructions are executed • Upon return - start over at Step 1 • If condition is met execution of statements continues with the statement following the LOOP Structured COBOL Programming, Stern & Stern, 9th Edition

  12. PERFORM UNTIL . . . END-PERFORM Statement: A Structured Programming Technique • The basic instruction format of the PERFORM UNTIL ... END- PERFORM statement is as follows: PERFORM UNTIL condition-1 . . . [END-PERFORM]* COBOL 85 only Structured COBOL Programming, Stern & Stern, 9th Edition

  13. THE SEQUENCE OF INSTRUCTIONS IN A PROGRAM • A PERFORM paragraph-name • An instruction that temporarily transfers control to another paragraph • Instructions are executed 1 time • Program control is returned to statement following the PERFORM • Program knows when to return when • A new PARAGRAPH is encountered or • No more program statements to execute Structured COBOL Programming, Stern & Stern, 9th Edition

  14. The Top-Down Approach for Coding Paragraphs • Well-designed programs are written using a top-down approach. • This means that the main module is coded first • subsequent modules are coded from the major level to the detail level. • Endeavor to code the more general paragraphs first and end with the most detailed ones. • Facilitates incremental testing • Write the paragraphs at one level • Lower level paragraphs at stubs (ie. No code) • Test • Proceed to write paragraphs at next level • Go step 2 Structured COBOL Programming, Stern & Stern, 9th Edition

  15. STATEMENTS TYPICALLY CODED IN THE MAIN MODULE OF BATCH PROGRAMS Structured COBOL Programming, Stern & Stern, 9th Edition

  16. OPEN Statement • The OPEN statement accesses the files in a program and indicates which are input and which are output. It has the following instruction format: • Format OPEN INPUTfile-name-1 . . . OUTPUTfile-name-2 . . . OPEN INPUTfile-name-1 ... OPEN OUTPUTfile-name-2 ... Structured COBOL Programming, Stern & Stern, 9th Edition

  17. OPEN OPENINPUT file-name-1 ... OUTPUT file-name-2 ... Structured COBOL Programming, Stern & Stern, 9th Edition

  18. OPEN STATEMENT • A REVIEW OF INSTRUCTION FORMAT SPECIFICATIONS 1. Uppercase words are COBOL reserved words. 2. Underlined words are required in the statement or option specified. 3. Lowercase entries are user-defined words. 4. Braces { } denote that at least one of the enclosed items is required. Structured COBOL Programming, Stern & Stern, 9th Edition

  19. OPEN STATEMENT • A REVIEW OF INSTRUCTION FORMAT SPECIFICATIONS 5. Brackets [ ] denote that the enclosed item is optional. 6. Punctuation, when included in the format, is required. 7. The use of three dots or ellipses (. . .) indicates that additional entries of the same type (a file-name in this case) may be repeated if desired. Structured COBOL Programming, Stern & Stern, 9th Edition

  20. OPEN STATEMENT • FUNCTIONS OF THE OPEN STATEMENT 1. Indicates which files will be input and which will be output. 2. Makes the files available for processing Locates INPUT file – if not found a runtime error Allocates space for the OUPUT filr 3. Performs header label routines if label records are STANDARD. Structured COBOL Programming, Stern & Stern, 9th Edition

  21. OPEN STATEMENT DEBUGGING TIPS: CODING GUIDELINES Indent each line within an OPEN sentence. • This makes a program more readable. • For the OPEN sentence, we typically indent so that the words INPUT and OUTPUT are aligned. For other entries we indent four spaces. Structured COBOL Programming, Stern & Stern, 9th Edition

  22. OPEN STATEMENT DEBUGGING TIPS: CODING GUIDELINES AN EXAMPLE: OPEN INPUTOLD-MASTER-IN TRANS-FILE OUTPUTNEW-MASTER-OUT ERROR-LIST. Structured COBOL Programming, Stern & Stern, 9th Edition

  23. OPEN STATEMENT DEBUGGING TIPS: CODING GUIDELINES • For output disk files, the ASSIGN clause of a SELECT statement often specifies the name the file is to be saved as: SELECTSALES-FILEASSIGN TO “A:\DATA100.DAT” ORGANIZATION IS LINE SEQUENTIAL. Structured COBOL Programming, Stern & Stern, 9th Edition

  24. READ Statement • Typically, after an input file has been opened, the PERFORM ... END-PERFORM loop, which begins with a READ, is executed. • A READ statement transmits data from the input device, assigned in the ENVIRONMENT DIVISION, to the input storage area, defined in the FILE SECTION of the DATA DIVISION. Structured COBOL Programming, Stern & Stern, 9th Edition

  25. READ Statement • The following is a partial instruction format for a READ statement: Format READ file-name-1 AT END statement-1 . . . [NOT AT END statement-2 . . .] [END-READ]* COBOL 85 ONLY Structured COBOL Programming, Stern & Stern, 9th Edition

  26. READ Statement • AT END • READ test to see if there are more records on the file if not the instruction(s) following the AT END clause are executed • NOT AT END • If a successful READ the instruction(s) following the NOT AT END are executed • Program execution then continues with the instruction following the END-READ Structured COBOL Programming, Stern & Stern, 9th Edition

  27. READ Statement • The file-name specified in the READ statement appears in three previous places in the program: 1. The SELECT statement, indicating the file-name and the device or implementor-name assigned to the file. • If a file is stored on a disk, for example, a READ operation transmits data from the disk to the input area. 2. The FD entry, describing the file and its format. 3. The OPEN statement, accessing the file and activating the device. Structured COBOL Programming, Stern & Stern, 9th Edition

  28. READ Statement • The primary function of the READ statement is to transmit one data record to the input area reserved for that file. • Each time a READ statement is executed, one record is read into primary storage - not the entire file. Structured COBOL Programming, Stern & Stern, 9th Edition

  29. DEBUGGING TIP • Code the AT END and NOT AT END clause on separate lines and indent them for readability. • If an error occurs, you will be able to more easily identify the problem because the line number of the error is specified. • Common programming error trying to continued to process records whrn END-OF-FILE has been encountered Structured COBOL Programming, Stern & Stern, 9th Edition

  30. QUESTIONS? Structured COBOL Programming, Stern & Stern, 9th Edition

  31. End-of-Job Processing: The CLOSE and STOP RUN Statements The CLOSE Statement • Files must be accessed or activated by an OPEN statement before data may be read or written. • Similarly, a CLOSE statement is coded at the end of the job after all records have been processed to release these files and deactivate the devices. Structured COBOL Programming, Stern & Stern, 9th Edition

  32. End-of-Job Processing: The CLOSE and STOP RUN Statements The format of the CLOSE is: CLOSEfile-name-1 . . . Structured COBOL Programming, Stern & Stern, 9th Edition

  33. End-of-Job Processing: The CLOSE and STOP RUN Statements The STOP RUN Statement • The STOP RUN instruction tells the computer to terminate the program. • All programs should include a STOP RUN statement to end the run. • The STOP RUN is usually the last instruction in the main module. Structured COBOL Programming, Stern & Stern, 9th Edition

  34. STATEMENTS TYPICALLY CODED FOR PROCESSING INPUT RECORDS AND PRODUCING OUTPUT RECORDS Structured COBOL Programming, Stern & Stern, 9th Edition

  35. Simplified MOVE Statement • A simple MOVE statement has the following basic instruction format: MOVEidentifier-1TOidentifier-2 • Fields in main memory may be moved to other fields with the use of the MOVE instruction. • The word ``identifier'' means ``data-name''. Structured COBOL Programming, Stern & Stern, 9th Edition

  36. WRITE Statement • The WRITE instruction takes data in the output area defined in the DATA DIVISION and transmits it to the device specified in the ENVIRONMENT DIVISION. • A simple WRITE statement has the following format: WRITErecord-name-1 Structured COBOL Programming, Stern & Stern, 9th Edition

  37. WRITE Statement Format WRITE record-name-1 AFTER ADVANCING n LINE(S) AFTER ADVANCING PAGE • Note that although files are read, we write records. • The record-name appears on the 01 level and is generally subdivided into fields. The record description specifies the format of the output. • With each WRITE instruction, we tell the computer to write data that is in the output area. Structured COBOL Programming, Stern & Stern, 9th Edition

  38. NOTE Carefully • You READ a file • You WRITE a record Structured COBOL Programming, Stern & Stern, 9th Edition

  39. LOOKING AHEAD: AN INTRODUCTION TO ARITHMETIC and CONDITIONAL verbs Structured COBOL Programming, Stern & Stern, 9th Edition

  40. The four basic arithmetic verbs have the following simple formats:: ADD {identifier-1} {literal-1} TO identifier-2 SUBTRACT {identifier-1} {literal-1} FROM identifier-2 MULTIPLY {identifier-1} {literal-1} BY identifier-2 DIVIDE {identifier-1} {literal-1} INTOidentifier-2 Result always stored in last identifier. Structured COBOL Programming, Stern & Stern, 9th Edition

  41. The four basic arithmetic verbs have the following another format ADD {identifier-1} {literal-1} TOidentifier-2 GIVING indentifer-3 SUBTRACT {identifier-1} {literal-1} FROM identifier-2 GIVING indentifer-3 MULTIPLY {identifier-1} {literal-1} BY identifier-2 GIVING indentifer-3 DIVIDE {identifier-1} {literal-1} INTOidentifier-2 GIVING indentifer-3 Result always stored in last identifier. Structured COBOL Programming, Stern & Stern, 9th Edition

  42. The basic instruction format for a conditional IF is as follows: Format IF condition-1 [THEN] imperative-statement-1. . . [ELSE imperative-statement-2 . . .] [END-IF] *Note that the ELSE clause is optional. Numerous statements can follow each IF or ELSE clause. Structured COBOL Programming, Stern & Stern, 9th Edition

  43. The simple conditions that can be tested are as follows: (identifier-1) = (or IS EQUAL TO) identifier-2 < (or IS LESS THAN) literal-1 > (or IS GREATER THAN) Structured COBOL Programming, Stern & Stern, 9th Edition

  44. REVIEW OF COMMENTS IN COBOL Structured COBOL Programming, Stern & Stern, 9th Edition

  45. COMMENTS IN COBOL • An asterisk (*) in column 7 (the continuation position) of any line makes the entire line a comment • Use comments freely to make your program user-friendly and easier to understand. Structured COBOL Programming, Stern & Stern, 9th Edition

  46. Coding Guidelines for PROCEDURE DIVISION Entries 1. Each clause should be on a separate line indented for readability. For example: READ ... AT END ... NOT AT END ... END-READ. Structured COBOL Programming, Stern & Stern, 9th Edition

  47. Coding Guidelines for PROCEDURE DIVISION Entries 2. Each paragraph-name should begin with a sequence number that helps to pinpoint the location of the paragraph: a descriptive name should follow this number (e.g., 100-MAIN-MODULE, 200-CALC-RTN). 3. The last statement in a paragraph should always end with a period. Structured COBOL Programming, Stern & Stern, 9th Edition

  48. COBOL 2000+ CHANGES • You will be able to code comments on a line, even those with instructions: *> will be used to add a comment to a line. • After *> appears, characters to the end of the line will not be compiled. Structured COBOL Programming, Stern & Stern, 9th Edition

  49. YEAR 2000-COMPLIANT DATE FIELDS • Older, legacy programs, using only two-digit for the year, will no longer accurately depict the date beginning in the year 2000. • Problems associated with dates after this are referred to as the Year 2000 Problem or Y2K Problem. Structured COBOL Programming, Stern & Stern, 9th Edition

  50. YEAR 2000-COMPLIANT DATE FIELDS • Fixes for the Y2K problem are not all that difficult: • They are just costly and time-consuming. • In addition to program source changes, all files on which they operate will also need to be modified. Structured COBOL Programming, Stern & Stern, 9th Edition

More Related