Presentation is loading. Please wait.

Presentation is loading. Please wait.

371 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.

Similar presentations


Presentation on theme: "371 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John."— Presentation transcript:

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

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

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

4 374 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+

5 375 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.

6 376 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

7 377 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!

8 378 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.

9 379 IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE AUTHOR. YOUR-NAME-PLEASE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-DATA ASSIGN TO EMP-DAT. SELECT PAYROLL-LISTING ASSIGN TO PRINTER. DATA DIVISION. FILE SECTION. FDEMPLOYEE-DATA. 01EMPLOYEE-RECORD. 05EMPLOYEE-NAME-INPIC X(20). 05HOURS-WORKED-INPIC 99. 05HOURLY-RATE-INPIC 9V99. FDPAYROLL-LISTING. 01PRINT-REC. 05PIC X(20). 05NAME-OUTPIC X(20). 05PIC X(10). 05HOURS-OUTPIC 99. 05PIC X(8). 05RATE-OUTPIC 9.99. 05PIC XXXXXX. 05WEEKLY-WAGES-OUTPIC 999.99. WORKING-STORAGE SECTION. 01ARE-THERE-MORE-RECORDSPIC XXX VALUE ‘YES’.

10 3710 PROCEDURE DIVISION. 100-MAIN-MODULE. OPEN INPUT EMPLOYEE-DATA OUTPUT PAYROLL-LISTING. PERFORM UNTIL ARE-THERE-MORE-RECORDS = ‘NO’ READ EMPLOYEE-DATA AT END MOVE ‘NO’ TO ARE-THERE-MORE-RECORDS NOT AT END PERFORM 200-WAGE-ROUTINE END-READ END-PERFORM CLOSE EMPLOYEE-DATA PAYROLL-LISTING STOP RUN. 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.

11 3711 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.

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

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

14 3714 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!

15 3715 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

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

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

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

19 3719 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

20 3720 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

21 3721  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

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

23 3723 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

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

25 3725 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:

26 3726 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.

27 3727 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”.

28 3728 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,…)

29 3729 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

30 3730 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.)

31 3731  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.

32 3732  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

33 3733 IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE AUTHOR. YOUR-NAME-PLEASE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-DATA ASSIGN TO EMP-DAT. SELECT PAYROLL-LISTING ASSIGN TO PRINTER. DATA DIVISION. FILE SECTION. FDEMPLOYEE-DATA. 01EMPLOYEE-RECORD. 05EMPLOYEE-NAME-INPIC X(20). 05HOURS-WORKED-INPIC 99. 05HOURLY-RATE-INPIC 9V99. FDPAYROLL-LISTING. 01PRINT-REC. 05PIC X(20). 05NAME-OUTPIC X(20). 05PIC X(10). 05HOURS-OUTPIC 99. 05PIC X(8). 05RATE-OUTPIC 9.99. 05PIC XXXXXX. 05WEEKLY-WAGES-OUTPIC 999.99. WORKING-STORAGE SECTION. 01ARE-THERE-MORE-RECORDSPIC XXX VALUE ‘YES’.

34 3734 PROCEDURE DIVISION. 100-MAIN-MODULE. 08OPEN INPUT EMPLOYEE-DATA 12OUTPUT PAYROLL-LISTING. PERFORM UNTIL ARE-THERE-MORE-RECORDS = ‘NO’ READ EMPLOYEE-DATA 16AT END 20MOVE ‘NO’ TO ARE-THERE-MORE-RECORDS NOT AT END PERFORM 200-WAGE-ROUTINE END-READ END-PERFORM CLOSE EMPLOYEE-DATA PAYROLL-LISTING STOP RUN. 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. …

35 3735 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!

36 3736 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


Download ppt "371 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John."

Similar presentations


Ads by Google