Difference between revisions of "COP 2121 Basic Cobol Program Structure"

From In The Wings
Jump to navigation Jump to search
 
Line 149: Line 149:
 
** Period used only at the end of the entire FD
 
** Period used only at the end of the entire FD
 
** Recommendation: any clause used in FD appear on a separate line for clarity and ease of debugging
 
** Recommendation: any clause used in FD appear on a separate line for clarity and ease of debugging
 +
=====RECORD CONTAINS clause=====
 +
* Indicates size of each record
 +
* Optional but recommended since it provides check on record size
 +
* Example
 +
<pre>
 +
FD Sales-File
 +
    Record contains 75 characters
 +
</pre>
 +
** If the size of the files in the record definition mistakenly add up to 76 or more, compiler will report a syntax error
 +
=====Defining a Record=====
 +
* Each FD followed by record description entries for the file
 +
* Record description entries specify:
 +
** Name, type and size of the fields to appear in the record
 +
** The order in which the fields appear
 +
** How these fields are related to one another
 +
=====Record Description Entries=====
 +
* Record description entries grouped by levels
 +
** Record-name defined at the 01 level and considered the highest level of data
 +
** Fields within a record defined at subordinate level with numbers from 02 to 49
 +
*** Fields with higher level numbers contain fields with lower level numbers
 +
*** Fields at same level numbers are independent items
 +
=====Record Description Example=====
 +
<pre>
 +
01 Employee-Rec-In.
 +
    05 Name-In ...
 +
    05 Annual-Salary-In ...
 +
    05 Job-Description-In ...
 +
</pre>
 +
* Fields at 05 level subordinate to 01 level entry
 +
* All fields at same level (05) independent or not subordinate to each other
 +
=====Invalid Level Numbers=====
 +
<pre>
 +
01 Employee-Rec-In.
 +
    05 Name-In ...
 +
        10 First-Name-In
 +
        12 Last-Name-In
 +
    05 Job-Description-In ...
 +
</pre>
 +
* Last-Name-In level number is invalid since it is not subordinate to First-Name-In
 +
* Both first and last name are at same level and should have same level number
 +
* This is considered a logic error, and would not error at the compilation level
 +
=====Elementary and Group Items=====
 +
* Items defined with a level number are one of two types
 +
** Elementary item - Field that is not further subdivided
 +
*** Must include a PICTURE clause to define its type and size
 +
** Group Item - Field that is further subdivided
 +
*** Has no PICTURE clause
 +
=====PICTURE (PIC) clauses=====
 +
* Specify type and size of the field
 +
* Three types:
 +
** Numeric: only digits (denoted by 9 in PIC clause)
 +
** Alphanumeric: letters, digits, special characters (denoted by X in PIC clause)
 +
** Alphabetic: Only letters and blanks (denoted by A in PIC clause)
 +
* Size:
 +
** Specified by number of digits, either explicitly written out (i.e. XXXX) or abbreviated (i.e. X(4))
 +
* Optional "IS"
 +
<pre>
 +
Cust-ID-In  PICTURE IS XXXX
 +
</pre>
 +
* Special characters, 'S', '.', & 'V'
 +
** "PICTURE IS S99V9" for a signed decimal
 +
** V stands for implied decimal, and should be used for all data fileds that will be used in arithmetic operations
 +
** '.' used if the data field is not involved in arithmetic operations and the decimal point needs to be printed
 +
* PICTURE Example
 +
<pre>
 +
01 Employee-Rec-In.
 +
  05 Name-In
 +
    10 First-Name-In      PIC X(8)
 +
    10 Last-Name-In      PIC X(8)
 +
  05 Annual-Salary-In    PIC 99999V99
 +
 +
----|----|----|----|---
 +
Bill    Hall    4000000
 +
</pre>
 +
=====Putting it all Together=====
 +
* SELECT defines a file-name for a physical file or device
 +
* FD describes the file
 +
* 01 names the file record
 +
* 02-49 describes fields within the file record
 +
=====Design Example=====
 +
* Problem Specification:
 +
** Given an input file containing a student's course record, output a file containing their GPA record
 +
* Course Record:
 +
** Includes Name, SSN, Courses, Grades
 +
* GPA Record:
 +
** Includes Name, SSN, GPA
 +
<pre>
 +
ENVIRONMENT DIVISION.
 +
INPUT-OUTPUT SECTION.
 +
FILE-CONTROL.
 +
SELECT COURSE-FILE
 +
              ASSIGN TO “CourseFile.in“
 +
                      ORGANIZATION IS LINE SEQUENTIAL.
 +
        SELECT GPA-FILE
 +
              ASSIGN TO “GPAFile.in“
 +
                      ORGANIZATION IS LINE SEQUENTIAL.
 +
 +
DATA DIVISION.
 +
FILE SECTION.
 +
 +
FD COURSE-FILE
 +
    RECORD CONTAINS 32 CHARACTERS.
 +
01 COURSE-RECORD.
 +
    05 FULL-NAME.
 +
      10 FIRST-NAME     PIC X(10).
 +
  10 LAST-NAME     PIC X(10).
 +
    05 SSN     PIC 9(9).
 +
    05 COURSE-GRADES.
 +
      10 COBOL     PIC X.
 +
      10 C     PIC X.
 +
      10 JAVA     PIC X.
 +
 +
FD GPA-FILE
 +
    RECORD CONTAINS 32 CHARACTERS.
 +
01 GPA-RECORD.
 +
    05 NAME     PIC x(20).
 +
    05 SSN     PIC 9(9).
 +
    05 GPA     PIC 9V99.
 +
</pre>
 +
=====Coding guidelines for Record Description Entries=====
 +
* Record-names are coded in Area A on the 01 level
 +
* All fields are coded on levels 02-49 in Area B
 +
* Subordinate or elementary entries are indented for clarity
 +
* Level numbers need not be consecutive; most programmers use increments of 05
 +
* By skipping levels, this leaves room for later insertions without having to rewrite the code

Revision as of 11:14, 23 February 2007

COBOL Program Structure

  • Each COBOL program is divided into four parts, called divisions:
    • Identification division
    • Environment division
    • Data division
    • Procedure division
  • Division can contain sections
  • Sections can contain paragraphs
  • Paragraphs contain statements
    • A statement or series of statements that end with a period are referred to as a sentence
  • Source program line has 80 character positions
  • Positions on line reserved for special purposes
    • Columns 1-6 and 73-80 optional and rarely used today
    • Column 7 for continuation, comment, starting a new page
    • Columns 8-72 for COBOL program statements
  • Column 7:
    • * (asterisk) designates entire line as comment
    • / (slash) forces page break when printing source listing
    • - (dash) to indicate continuation of nonnumberic literal
  • Columns 8-72 divided into two areas
    • Area A - columns 8-11
    • Area B - columns 12-72

Margin Rules

  • Division Names
    • Examples:
      • IDENTIFICATION DIVISION.
      • ENVIRONMENT DIVISION.
      • DATA DIVISION.
      • PROCEDURE DIVISION.
    • Beign in Area A, end with a period
      • First letter of name must begin in column 8-11
      • Entry may extend into Area B
    • Must appear on a line with no other entries
  • Section Names
    • Examples:
      • INPUT-OUTPUT SECTION.
      • FILE SECTION.
      • WORKING-STORAGE SECTION.
    • Being in Area A, end with a period
      • First letter of name must begin in column 8-11
      • Entry may extend into Area B
    • Must appear on a line with no other entries
  • Paragraph-names
    • Examples:
      • PROGRAM-ID.
      • MAIN-ROUTINE.
    • Being in Area A, end with a period followed by at least one space
    • May appear on line by themselves or with other entries. Recommended to be put by themselves for clarity.
  • Statements and Sentences
    • Examples:
      • SELECT EMPLOYEE-DATA ASSIGN TO "EMP.DAT"
      • COMPUTE WAGES = HOURES*RATE
    • Being in Area B (Column 12-72)
    • May appear on line by themselves or with other entries
    • Statements may end with period but not recommended
    • Sentences (e.g. a paragraph made up of one or more statements) end with period followed by at least one space
  • Summary
    • Division, section and paragraph names begin in Area A
    • All other statements begin anywhere in Area B
    • Division and section names end with a period
    • Paragraph-names and Sentences end with period followed by at least one space

Identification Division

  • Has no effect on the execution of a program, but is required
  • Provides identifying information about program
  • Divided into following paragraphs:
    • PROGRAM-ID. program-name. (required entry)
    • AUTHOR.
    • INSTALLATION.
    • DATE-WRITTEN.
    • DATE-COMPILED.
    • SECURITY.
  • program-name can be up to eight characters, letters and digits only. Acceptable on all computers.
  • PROGRAM-ID starts in Area A
  • program-name starts in Area B

Environment Division

  • Only machine-dependent division
    • May change if program is run on a different computer
  • Supplies information about the computer equipment to be used in the program
    • Describes files and computer devices used to process them
  • Optional for COBOL 85, but is required if files are to be processed

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

Input-Output Division

  • Follows Configuration section (if coded)
  • Included File-control paragraph
    • Consists of SELECT statements
    • One SELECT statement for each file used by program
    • Each SELECT defines a file-name and assigns device name to that file
    • Each SELECT statement starts in Area B

SELECT Statement file-names

  • File-names
    • are user-defined words chosen by programmer
    • Must follow rules for forming user-defined words

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 a COBOL reserved word

SELECT implementor-names

  • Implementor-name is the path to the file
  • Conventions for these names vary widely among computers

ORGANIZATION clause

  • This clause describes organization of records in the file
  • Most disk files created as text files
    • Following data for each record, Enter key is pressed
  • Use ORGANIZATION IS LINE SEQUENTIAL to
    • Correctly read records from files when Enter key is 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

Coding Guidelines

  1. Code SELECT statements in logical order (input files first, then output files) although order is not required
  2. Use separate lines for SELECT, ASSIGN, ORGANIZATION clauses for readability
  3. Choose meaningful file-names
  • EMPLOYEE-FILE instead of E-FILE
  • EMP-REPORT-FILE instead of OUT-FILE

Data Division

  • Follows Environment Division
  • Defines and describes storage for all data
    • Files, records, fields in batch programs
    • Fields keyed in by the user or displayed on screen to the user in interactive programs
    • Any other data fields used by the program
  • Each data item is assigned a user-defined name
  • Data item names must follow the rules for user-defined name
    • Use meaningful data-names that describe contents of field
    • Use prefixes or suffixes in data-names when appropriate

Data Division Sections

  • Two main sections
    • File section (required if program uses files)
      • Defines all input and output files and their record formats
    • Working-storage section
      • Defines all other fields not part of input or output files but used by the program
  • File Section must be described first, followed by the working-storage section

File Section

  • Each file described with an FD (File Descripor) sentence
  • One FD for each SELECT statement in Environment Division
  • Format
FD file-name
     RECORD CONTAINS integer-1 CHARACTERS.
    • file-name used in FD statement is the same as the file-name used in SELECT statement
    • FD is coded in Area A
    • RECORD CONTAINS should be coded in Area B
    • Period used only at the end of the entire FD
    • Recommendation: any clause used in FD appear on a separate line for clarity and ease of debugging
RECORD CONTAINS clause
  • Indicates size of each record
  • Optional but recommended since it provides check on record size
  • Example
FD Sales-File
    Record contains 75 characters
    • If the size of the files in the record definition mistakenly add up to 76 or more, compiler will report a syntax error
Defining a Record
  • Each FD followed by record description entries for the file
  • Record description entries specify:
    • Name, type and size of the fields to appear in the record
    • The order in which the fields appear
    • How these fields are related to one another
Record Description Entries
  • Record description entries grouped by levels
    • Record-name defined at the 01 level and considered the highest level of data
    • Fields within a record defined at subordinate level with numbers from 02 to 49
      • Fields with higher level numbers contain fields with lower level numbers
      • Fields at same level numbers are independent items
Record Description Example
01 Employee-Rec-In.
    05 Name-In ...
    05 Annual-Salary-In ...
    05 Job-Description-In ...
  • Fields at 05 level subordinate to 01 level entry
  • All fields at same level (05) independent or not subordinate to each other
Invalid Level Numbers
01 Employee-Rec-In.
    05 Name-In ...
        10 First-Name-In
        12 Last-Name-In
    05 Job-Description-In ...
  • Last-Name-In level number is invalid since it is not subordinate to First-Name-In
  • Both first and last name are at same level and should have same level number
  • This is considered a logic error, and would not error at the compilation level
Elementary and Group Items
  • Items defined with a level number are one of two types
    • Elementary item - Field that is not further subdivided
      • Must include a PICTURE clause to define its type and size
    • Group Item - Field that is further subdivided
      • Has no PICTURE clause
PICTURE (PIC) clauses
  • Specify type and size of the field
  • Three types:
    • Numeric: only digits (denoted by 9 in PIC clause)
    • Alphanumeric: letters, digits, special characters (denoted by X in PIC clause)
    • Alphabetic: Only letters and blanks (denoted by A in PIC clause)
  • Size:
    • Specified by number of digits, either explicitly written out (i.e. XXXX) or abbreviated (i.e. X(4))
  • Optional "IS"
Cust-ID-In   PICTURE IS XXXX
  • Special characters, 'S', '.', & 'V'
    • "PICTURE IS S99V9" for a signed decimal
    • V stands for implied decimal, and should be used for all data fileds that will be used in arithmetic operations
    • '.' used if the data field is not involved in arithmetic operations and the decimal point needs to be printed
  • PICTURE Example
01 Employee-Rec-In.
  05 Name-In
    10 First-Name-In      PIC X(8)
    10 Last-Name-In       PIC X(8)
  05 Annual-Salary-In     PIC 99999V99

----|----|----|----|---
Bill    Hall    4000000
Putting it all Together
  • SELECT defines a file-name for a physical file or device
  • FD describes the file
  • 01 names the file record
  • 02-49 describes fields within the file record
Design Example
  • Problem Specification:
    • Given an input file containing a student's course record, output a file containing their GPA record
  • Course Record:
    • Includes Name, SSN, Courses, Grades
  • GPA Record:
    • Includes Name, SSN, GPA
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL. 	
	 SELECT COURSE-FILE 
	               ASSIGN TO “CourseFile.in“
                      ORGANIZATION IS LINE SEQUENTIAL.
         SELECT GPA-FILE 
	               ASSIGN TO “GPAFile.in“
                      ORGANIZATION IS LINE SEQUENTIAL.
		
DATA DIVISION.
FILE SECTION.

FD COURSE-FILE
    RECORD CONTAINS 32 CHARACTERS.
01 COURSE-RECORD.
    05 FULL-NAME.
      10 FIRST-NAME	    PIC X(10).
	   10 LAST-NAME	    PIC X(10).
    05 SSN		    PIC 9(9).
    05 COURSE-GRADES.
      10 COBOL		    PIC X.
      10 C		    PIC X.
      10 JAVA		    PIC X.

FD GPA-FILE
    RECORD CONTAINS 32 CHARACTERS.
01 GPA-RECORD.
    05 NAME		     PIC x(20).
    05 SSN		     PIC 9(9).
    05 GPA		     PIC 9V99.
Coding guidelines for Record Description Entries
  • Record-names are coded in Area A on the 01 level
  • All fields are coded on levels 02-49 in Area B
  • Subordinate or elementary entries are indented for clarity
  • Level numbers need not be consecutive; most programmers use increments of 05
  • By skipping levels, this leaves room for later insertions without having to rewrite the code