1 / 37

Structured COBOL Programming

Structured COBOL Programming. 11th edition. John Wiley & Sons, Inc . Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout. PowerPoint Winifred J. Rex Presentation Bowling Green State University. Chapter 2.

cira
Download Presentation

Structured COBOL Programming

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. Structured COBOL Programming 11th edition John Wiley & Sons, Inc. Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout PowerPoint Winifred J. Rex Presentation Bowling Green State University 37

  2. Chapter 2 The IDENTIFICATION and ENVIRONMENT DIVISIONs Modified by your instructor for pedagogical reasons only. Spring 2006 37

  3. Chapter Contents • Basic Structure of a COBOL Program • Coding Requirements of IDENTIFICATION DIVISION • Sections of ENVIRONMENT DIVISION • Assigning Files to Devices in ENVIRONMENT DIVISION 37

  4. Basic COBOL Program Structure • Originally, each COBOL instruction coded on single line of 80 characters • Positions on line reserved for special purposes • Rules may differ for your compiler • Rigid column rules dropped in 2002+ 37

  5. Coding Rules • Columns 1-6 and 73-80 optional and rarely used today • statement numbers; program-id… • Column 7 for continuation, comment, starting new page • Symbols are dash (-), asterisk (*) and slash (/) • Columns 8-72 for COBOL program statements • This is where NetExpress positions you. 37

  6. Margin Rules • Columns 8-72 divided into two areas • Area A - columns 8, 9, 10, 11 • Area B - columns 12-72 • Division, section and paragraph-names must all begin in Area A • First letter of name must begin in column 8, 9, 10 or 11 • Entry may extend into Area B 37

  7. Margin Rules • All other statements, clauses, and sentences begin anywhere in Area B (column 12, 13, 14, etc.) • Select entries in ENVIRONMENT DIVISION • Data description entries in DATA DIVISION • All PROCEDURE DIVISION instructions • Most will start in column 12! 37

  8. Review of Margin Rules • Division and Section Names • Begin in Area A, end with a period • Must appear on a line with no other entries • Paragraph-names • Begin in Area A, end with period followed by space • May appear on line by themselves or with other entries • Example: 2000-Wage-Routine. 37

  9. IDENTIFICATION DIVISION. <alignment and indentation!> PROGRAM-ID. SAMPLE <divisions, sections, paragraphs, sentences statements> AUTHOR. YOUR-NAME-PLEASE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-DATA ASSIGN TO EMP-DAT. SELECT PAYROLL-LISTING ASSIGN TO PRINTER. <will change> DATA DIVISION. <input and output files and formats; working storage…> FILE SECTION. FD EMPLOYEE-DATA. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-NAME-IN PIC X(20). 05 HOURS-WORKED-IN PIC 99. 05 HOURLY-RATE-IN PIC 9V99. <alignment; pic clauses> FD PAYROLL-LISTING. 01 PRINT-REC. 05 PIC X(20). 05 NAME-OUT PIC X(20). 05 PIC X(10). 05 HOURS-OUT PIC 99. 05 PIC X(8). 05 RATE-OUT PIC 9.99. 05 PIC XXXXXX. 05 WEEKLY-WAGES-OUT PIC 999.99. WORKING-STORAGE SECTION. 01 ARE-THERE-MORE-RECORDS PIC XXX VALUE ‘YES’. <Value clause> 37

  10. PROCEDURE DIVISION. <instructions to operate (read. print) / manipulate data> 100-MAIN-MODULE. OPEN INPUT EMPLOYEE-DATA <must have for files> OUTPUT PAYROLL-LISTING. PERFORM UNTIL ARE-THERE-MORE-RECORDS = ‘NO’ <loop construct> READ EMPLOYEE-DATA <note the indentation!!!> AT END MOVE ‘NO’ TO ARE-THERE-MORE-RECORDS NOT AT END PERFORM 200-WAGE-ROUTINE END-READ <note the ‘control’ here – not detail!> END-PERFORM <note: scope terminators end-read; end-perform> CLOSE EMPLOYEE-DATA PAYROLL-LISTING STOP RUN. <last logical executable statement in program> 200-WAGE-ROUTINE. MOVE SPACES TO PRINT-REC MOVE EMPLOYEE-NAME-IN TO NAME-OUT MOVE HOURS-WORKED-IN TO HOURS-OUT MOVE HOURLY-RATE-IN TO RATE-OUT MULTIPLY HOURS-WORKED-IN BY HOURLY-RATE-IN GIVING WEEKLY-WAGES-OUT WRITE PRINT-REC. 37

  11. Review of Margin Rules • Statements and Sentences • Begin in Area B • May appear on line by themselves or with other entries. Us: one statement per line • Statements (e.g., OPEN, WRITE) may end with period but not recommended • Sentences (e.g., a paragraph made up of one or more statements) end with period followed by space •  The period is used to terminate a sentence. It terminates any conditional statement too, such as an IF, Read, some arithmetic statements, and a few other statements that may have conditional parts. •  We now prefer Scope Terminators where practical. 37

  12. IDENTIFICATION DIVISION • Provides identifying information about program • Divided into paragraphs • PROGRAM-ID only required paragraph • Other paragraphs optional 37

  13. Format IDENTIFICATIONDIVISION. PROGRAM-ID. program-name. [AUTHOR. [comment-entry] …] [other optional paragraphs] IDENTIFICATION DIVISION 37

  14. Backus-Normal Form (BNF) Brackets  optional Braces  chose from within Underlined  key word or special meaning Must include this entry (unless enclosed in brackets / braces. Once ‘inside’ the brackets and braces, if underlined word, must use.) Uppercase  Reserved Word. If used, must use as spelled. lower case  user/programmer-defined entry. ellipses  may repeat preceding item. All items may be nested! 37

  15. Understanding Instruction Formats • Instruction formats describe syntax of all parts of COBOL language • Describe required and optional elements, ordering and punctuation • All formats in text are correct although additional options may be available 37

  16. Rules for Instruction Formats • Uppercase words are COBOL reserved words • Lowercase words are user-defined entries IDENTIFICATIONDIVISION. PROGRAM-ID. program-name. • DIVISION is reserved word • program-name is user-defined data-name 37

  17. Rules for Instruction Formats • Underlined words are required • Punctuation if specified is required IDENTIFICATIONDIVISION. PROGRAM-ID. program-name. • IDENTIFICATION, DIVISION required • PROGRAM-ID is required paragraph • Periods required after division header, paragraph name and program-name Example 37

  18. Rules for Instruction Formats • Brackets [ ] mean item is optional, braces { } mean one of enclosed items required • Ellipses (. . .) mean entry may be repeated IDENTIFICATIONDIVISION. PROGRAM-ID. program-name. [AUTHOR. [comment-entry] …] • AUTHOR paragraph optional • If included it may have any number of comment entries Example 37

  19. ENVIRONMENT DIVISION • Describes files and computer devices used to process them • Required by programs that process files • This division is machine-dependent since devices differ from computer to computer •  Is the only division that may change if program run on different computer 37

  20. Sections of Environment Division • CONFIGURATION SECTION • Describes computer used to compile/execute program • Optional and recommended that you omit it • Paragraphs: Source-Computer. Object-Computer; Special-Names are found in here. •  INPUT-OUTPUT SECTION • Describes input and output files and devices used by program • Required for all programs using files 37

  21.  INPUT-OUTPUT SECTION • Follows CONFIGURATION SECTION (if coded) • Includes FILE-CONTROL paragraph • Contains one SELECT statement for each file used by program •  Each SELECT defines a file-name and assigns device name to that file 37

  22. Format INPUT-OUTPUTSECTION. Note: A-Margin FILE-CONTROL. SELECT file-name-1 ASSIGNTO implementor-name-1 [ORGANIZATION IS LINESEQUENTIAL].1 Notice the indentation of the Select… Notice the syntax of the statement! 1Use this clause for all PC files so each line treated as separate record. INPUT-OUTPUT SECTION 37

  23. SELECT Statement file-names • Choose meaningful file-names • EMPLOYEE-FILE instead of E-FILE • EMP-REPORT-FILE instead or OUT-FILE • File-names are user-defined words • Words chosen by programmer to represent some element of program • Many call these ‘logical’ file names or ‘internal file names’ or programmer-defined file names… • Must follow rules for forming user-define words 37

  24. Rules for User-Defined Words • 1 to 30 characters • Letters, digits, hyphens (-) only • No embedded blanks • At least one alphabetic character • May not begin or end with hyphen • May not be COBOL reserved word 37

  25. SELECT implementor-names • Conventions for these names vary widely among computers • Most enable use of special device names for frequently used devices Printer SYSLST, SYS$OUT, PRINTER Disk DISC or DISK and disk file-name In our class, we will merely use file names. Discuss: 37

  26. SELECT implementor-names • Special device-names example Select Transaction-File Assign to "Data1.dat". Select Report-File Organization is Line Sequential Assign to “Outfile.dat” (will discuss) IBM Mainframe? Assign to ut-s-ddname or da-i-ddname etc. 37

  27. SELECT Statements for PCs • For PCs, use device names specifying • Drive on which file appears followed by a colon • Folder name if file is in a folder (nested…) • Name of file (See emails on this) • PC example Select Inventory-File Assign To "C:\Inventory\Inv-File.dat". Us: Assign to C:\cop2120\p1\broggio\pgm2.cbl”. 37

  28. ORGANIZATION clause for PCs • This clause describes organization of records in the file • Most PC disk files are created as text files • Following data for each record, Enter key is pressed • Indicates end of the line and end of the record • If records 80 characters or less, each record appears on single line on screen or printer • (Other ‘organizations’: Index, Relative,…) 37

  29. ORGANIZATION clause for PCs • Include LINE SEQUENTIAL to • Correctly read records from files when Enter key used to mark the end of each record • Create disk files with each record followed by Enter key so each record appears on separate line when printed 37

  30. ORGANIZATION clause for PCs PC Example Select Sales-File Assign to "C:\Chapter2\Sales.dat" Organization is Line Sequential. (Our ‘path’ will be different, but format is same as above.) 37

  31.  Coding Guidelines 1. Separate divisions by blank comment line, page eject symbol or blank line Us: Don’t use page eject. Will discuss. 2. Code a single statement per line 3. Code paragraph-names on line by themselves 4. Be liberal in use of comments. Box lengthy comments using asterisks. 37

  32.  Coding Guidelines 5. Code SELECT statements in logical order (input files first, then output files) although order not required 6. Use separate lines for SELECT, ASSIGN, ORGANIZATION clauses for readability 7. Avoid use of device-specific file-names 37

  33. IDENTIFICATION DIVISION. <alignment and indentation!> PROGRAM-ID. SAMPLE <divisions, sections, paragraphs, sentences statements> AUTHOR. YOUR-NAME-PLEASE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-DATA ASSIGN TO EMP-DAT. SELECT PAYROLL-LISTING ASSIGN TO PRINTER. <will change> DATA DIVISION. <input and output files and formats; working storage…> FILE SECTION. FD EMPLOYEE-DATA. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-NAME-IN PIC X(20). 05 HOURS-WORKED-IN PIC 99. 05 HOURLY-RATE-IN PIC 9V99. <alignment; pic clauses> FD PAYROLL-LISTING. 01 PRINT-REC. 05 PIC X(20). 05 NAME-OUT PIC X(20). 05 PIC X(10). 05 HOURS-OUT PIC 99. 05 PIC X(8). 05 RATE-OUT PIC 9.99. 05 PIC XXXXXX. 05 WEEKLY-WAGES-OUT PIC 999.99. WORKING-STORAGE SECTION. 01 ARE-THERE-MORE-RECORDS PIC XXX VALUE ‘YES’. <Value clause> 37

  34. PROCEDURE DIVISION. <instructions to operate (read. print) / manipulate data> 100-MAIN-MODULE. 08 OPEN INPUT EMPLOYEE-DATA <must have for files> 12 OUTPUT PAYROLL-LISTING. PERFORM UNTIL ARE-THERE-MORE-RECORDS = ‘NO’ <loop construct> READ EMPLOYEE-DATA <  note the indentation!!!> 16 AT END 20 MOVE ‘NO’ TO ARE-THERE-MORE-RECORDS NOT AT END PERFORM 200-WAGE-ROUTINE END-READ <note the ‘control’ here – not detail!> END-PERFORM <note: scope terminators end-read; end-perform> CLOSE EMPLOYEE-DATA PAYROLL-LISTING STOP RUN. <last logical executable statement in program> 200-WAGE-ROUTINE. MOVE SPACES TO PRINT-REC MOVE EMPLOYEE-NAME-IN TO NAME-OUT MOVE HOURS-WORKED-IN TO HOURS-OUT MOVE HOURLY-RATE-IN TO RATE-OUT MULTIPLY HOURS-WORKED-IN BY HOURLY-RATE-IN GIVING WEEKLY-WAGES-OUT WRITE PRINT-REC. Discuss the indentation / use of tabs. … 37

  35. COBOL 2002+ Changes • Coding rules for Margins A and B will be recommended not required. Required • PROGRAM-ID will be only paragraph in IDENTIFICATION DIVISION. All others can be specified as comments. • Add Author paragraph and Date-Compiled. • Length of user-defined words will be increased from 30 to 60 characters. • Should never need that many! 37

  36. Chapter Summary • IDENTIFICATION DIVISION • Defines program name • Program name up to eight characters, letters and digits only, acceptable on all computers • PROGRAM-ID is only required paragraph, all others optional • Use comments by coding an * in column 7 37

  37. Chapter Summary • ENVIRONMENT DIVISION • Division is optional for COBOL 85 • Not needed for fully interactive programs • INPUT-OUTPUT SECTION required for any program using files • Only machine-dependent division since device specification, file-name rules vary among computers 37

More Related