Download presentation
Presentation is loading. Please wait.
Published byVivian Franklin Modified over 9 years ago
1
2-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus) John Wiley & Sons, Inc. 11th edition
2
2-2 The IDENTIFICATION and ENVIRONMENT DIVISIONs Chapter 2
3
2-3 Chapter Objectives To familiarize you with Basic structure of a COBOL program General coding and format rules IDENTIFICATION and ENVIRONMENT DIVISION entries
4
2-4 Chapter Contents Basic Structure of a COBOL Program Coding Requirements of IDENTIFICATION DIVISION Sections of ENVIRONMENT DIVISION Assigning Files to Devices in ENVIRONMENT DIVISION
5
CONTENTS Basic Structure of a COBOL Program –Coding a Source Program –Coding Rules The Main Body of a Program Optional Entries Identification and Page and Serial Numbers Column 7 : For Comments, Continuing Nonnumeric Literals, and Starting a New Page Area A and B –Types of COBOL Entries
6
CONTENTS Coding Requirements of the IDENTIFICATION DIVISION –Paragraphs in the IDENTIFICATION DIVISION
7
2-7 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 2008
8
COBOL Coding Sheet Structured COBOL Programming, Stern & Stern, 9th Edition
9
Coding a Source Program Traditional COBOL coding sheets have 20 lines with spaces set aside in columns 4--6 for sequence numbers. Each COBOL instruction is coded on a single line using 80 characters per line.
10
COBOL Coding Sheet Used to create an initial version of the program prior to keying it into the computer
11
Structured COBOL Programming, Stern & Stern, 9th Edition
12
COBOL Coding Sheet Structured COBOL Programming, Stern & Stern, 9th Edition
13
Coding a Source Program Every line written on the coding sheet should be keyed in with one line each on a terminal or PC keyboard. The entire program is referred to as the COBOL source program.
14
2-14 Coding Rules Columns 1-6 and 73-80 optional and rarely used today Column 7 for continuation, comment, starting new page Columns 8-72 for COBOL program statements
15
2-15 Coding Rules Column 7 * (asterisk) designates entire line as comment / (slash) forces page break when printing source listing - (dash) to indicate continuation of nonnumeric literal
16
Comments in a COBOL program
17
2-17
18
2-18
19
2-19 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
20
2-20 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
21
2-21
22
2-22
23
Areas A and B
24
2-24 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
25
2-25 Review of Margin Rules Statements and Sentences –Begin in Area B –May appear on line by themselves or with other entries –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
26
2-26 IDENTIFICATION DIVISION Provides identifying information about program Divided into paragraphs PROGRAM-ID only required paragraph Other paragraphs optional
27
2-27 IDENTIFICATION DIVISION Format IDENTIFICATION DIVISION. PROGRAM-ID. program-name. [AUTHOR. [comment-entry] …] [other optional paragraphs]
28
Paragraphs in the IDENTIFICATION DIVISION IDENTIFICATION DIVISION. PROGRAM-ID. program-name. [AUTHOR. [comment-entry]...] [INSTALLATION. [comment-entry]...] [DATE-WRITTEN. [comment-entry]...] [DATE-COMPILED. [comment-entry]...] [SECURITY. [comment-entry]...]
30
2-30
31
2-31 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
32
2-32 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 Example
33
2-33 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
34
2-34 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
35
2-35 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 Only division that may change if program run on different computer
36
2-36 Sections of Environment Division CONFIGURATION SECTION –Describes computer used to compile/execute program –Optional and recommended that you omit it INPUT-OUTPUT SECTION –Describes input and output files and devices used by program –Required for all programs using files
37
THE CONFIGURATION SECTION –Supplies information about the computer on which the COBOL program will be compiled and executed. SOURCE-COMPUTER: –The computer that will be used for compiling the program. OBJECT-COMPUTER: - The computer that will be used for executing or running the program. *SOURCE- COMPUTER and OBJECT-COMPUTER are coded primarily as documentation entries.
38
CONFIGURATION SECTION EXAMPLE
39
2-39 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
40
INPUT-OUTPUT SECTION 2-40
41
2-41 INPUT-OUTPUT SECTION
42
2-42 INPUT-OUTPUT SECTION Format INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name-1 ASSIGN TO implementor-name-1 [ORGANIZATION IS LINE SEQUENTIAL]. 1 1 Use this clause for all PC files so each line treated as separate record.
43
2-43 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 Must follow rules for forming user-define words
44
2-44 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
45
2-45 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
47
2-47 SELECT implementor-names Special device-names example Select Transaction-File Assign to Disk "Data1". Select Report-File Assign to Printer. Some systems use name of file stored on disk (VAX or Alpha systems) Select Sales-File Assign to "Sales1".
48
2-48 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 –Name of file PC example Select Inventory-File Assign To "C:\Inventory\Inv-File.dat".
49
ENVIRONMENT DIVISION Structured COBOL Programming, Stern & Stern, 9th Edition 000260 ENVIRONMENT DIVISION. 000270 CONFIGURATION SECTION. 000280 SOURCE-COMPUTER. IBM PC. 000290 OBJECT-COMPUTER. IBM PC. 000300 INPUT-OUTPUT SECTION. 000310 FILE-CONTROL. 000320 SELECT INPUT-FILE ASSIGN TO 'input.dat' 000330 ORGANIZATION IS LINE SEQUENTIAL. 000340 SELECT PRINT-FILE ASSIGN TO PRINTER.
50
ENVIRONMENT DIVISION INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT NAME-FILE ASSIGN TO C:\TMP\SAMPIN.TXT" ORGANIZATION IS LINE SEQUENTIAL. SELECT PRINT-FILE ASSIGN TO C:\TMP\SAMPOUT.TXT". SELECT SORT-FILE ASSIGN TO "C:\TMP\SORT.TMP".
51
2-51 ORGANIZATION clause for PCs This clause describes organization of records in the file Most PC disk files 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
52
2-52 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
53
2-53 ORGANIZATION clause for PCs PC Example Select Sales-File Assign to "C:\Chapter2\Sales.dat" Organization is Line Sequential.
54
2-54 Coding Guidelines 1. Separate divisions by blank comment line, page eject symbol or blank line 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.
55
2-55 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
56
2-56 COBOL 2008 Changes Coding rules for Margins A and B will be recommended not required. PROGRAM-ID will be only paragraph in IDENTIFICATION DIVISION. All others can be specified as comments. Length of user-defined words will be increased from 30 to 60 characters.
57
2-57 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
58
2-58 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
59
2-59 Copyright © 2003 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express written permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.
60
QUESTIONS?
61
SELF-TEST 1. If an entry must begin in Area A, it may begin in position _____ ; if an entry must begin in Area B, it may begin in position _____. Solution: 8, 9, 10, or 11; 12, 13, 14, and so on Structured COBOL Programming, Stern & Stern, 9th Edition
62
SELF-TEST 2. The four divisions of a COBOL program must appear in order as _____ ; _____ ; _____ ; and, _____. Solution: IDENTIFICATION; ENVIRONMENT; DATA; PROCEDURE Structured COBOL Programming, Stern & Stern, 9th Edition
63
SELF-TEST 3. What entries must be coded beginning in Area A? Solution: Division, section, and paragraph-names Structured COBOL Programming, Stern & Stern, 9th Edition
64
SELF-TEST 4. Most entries such as PROCEDURE DIVISION instructions are coded in Area _____. Solution: B Structured COBOL Programming, Stern & Stern, 9th Edition
65
SELF-TEST 5. _____ and _____ must each appear on a separate line. All other entries may have several statements on the same line. Solution: Division names; section names Structured COBOL Programming, Stern & Stern, 9th Edition
66
SELF-TEST 6. The first two entries of a COBOL program must always be _____ and _____. Solution: IDENTIFICATION DIVISION. PROGRAM-ID. program-name. Structured COBOL Programming, Stern & Stern, 9th Edition
67
SELF-TEST 7. Each of the preceding entries must be followed by a _____, which, in turn, must be followed by a ____. Solution: period; space or blank Structured COBOL Programming, Stern & Stern, 9th Edition
68
SELF-TEST 8. The first two entries of a program are both coded beginning in Area _____. Solution: A Structured COBOL Programming, Stern & Stern, 9th Edition
69
SELF-TEST 9. Code the IDENTIFICATION DIVISION for a program called EXPENSES for a corporation, Dynamic Data Devices, Inc., written July 15, 1997. This program has a security classification and is available to authorized personnel only. It produces a weekly listing by department of all operating expenses. Structured COBOL Programming, Stern & Stern, 9th Edition
70
SELF-TEST 9. Suggested solution: IDENTIFICATION DIVISION. PROGRAM-ID. EXPENSES. AUTHOR. N. B. STERN. INSTALLATION. DYNAMIC DATA DEVICES, INC. DATE-WRITTEN. 7/15/97. DATE-COMPILED. SECURITY. AUTHORIZED PERSONNEL ONLY. Structured COBOL Programming, Stern & Stern, 9th Edition
71
SELF-TEST 10. The DATE-COMPILED paragraph usually does not include a comment entry because _____. Solution: the computer itself can supply the date of the compilation (The current date is stored in main memory.) Structured COBOL Programming, Stern & Stern, 9th Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.