COP 2121 Basic Cobol Program Structure

From In The Wings
Jump to navigation Jump to search

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

WORKING-STORAGE Section

  • Follows FILE SECTION
  • Starts in Area A, ends with a period
  • Defines name, type and size of all other fields (that are not part of input or output files) used by the program
    • Example: Data fields for calculations, flags, counters, constants, etc.
  • Field names must follow the rules for user-defined names
    • For fields not used by program
      • Data-name may be left blank (preferable)
      • May use reserved word FILLER as data-name
      • Size must still be defined using PIC clause
  • All fields must be defined at 01 level or in entries subordinate to 01 level entry
  • All elementary items (or fields)
    • Must include PICTURE clause to define their type and size
    • Can be given optional initial valuse with VALUE clauses
VALUE clauses
  • Used to specify an initial value for data fields defined in the WS section
  • Example:
01	Total		PIC 9(5) 	VALUE    initial-value1
01	First-Name	PIC X(8) 	VALUE    initial-value2
    • initial-value1 and initial-value2 are constant (also called literals)
  • Constants or literals can be:
    • Numeric, 1 to 18 digits
    • Non-numeric, 1 to 160 characters
    • Figurative: SPACE(S), ZERO(S), QUOTE(S)
Rules for Numeric Literals
  1. 1 to 18 digits
  2. + or - sign may be included to left of first digit
  3. Decimal point permitted within literal. May not follow last digit.
Rules for Nonnumeric Literals
  • Must be enclosed in quotation marks
  • From 1 to 160 characters, including spaces
  • Any character in COBOL character set except double quotation mark (")
  • May contain all numbers ('125') but not same as numeric literal (125)
    • Cannot be used in arithmetic operations
    • Cannot be moved to field with PIC of 9's
  • Are not data-names
    • 'Amount-In' not same as field defined in DATA DIVISION called Amount-In
Figurative Constants
  • ZERO, ZEROS or ZEROES means all zeros
  • SPACE or SPACES means all spaces or blanks
VALUE Clause
  • Literal type must match the field type as defined by PICTURE clause
    • ZEROS may be used with both PIC X and PIC 9 fields
    • SPAECS may be used only wth alphanumeric fields
    • Numeric literals may be used only with PIC 9 fields
    • Nonnumeric literals may be used only with PIC X fields
  • If omitted, field's value undefined when program begins execution
  • May be used only in WORKING-STORAGE SECTION and not in FILE SECTION
  • Recommendation: Always initialize the data fields defined in WS section using VALUE clause
Continuation of Literals
  • Nonnumeric literals may be up to 160 characters
  • Long literals may not fit on one typed line
  • Long literals may be continued from one line to next, although this is not recommended
  • Rules for continuation of literals from one line to next
    • Begin literal with quotation marks
    • Continue literal to position 72. Do not end with quotation mark
    • Place hyphen on next line in position 7.
    • Continue literal with quotation mark starting anywhere in Area B
    • End literal with quotation mark
    • Subdividing long literals into seperate fields is recommended
 01  COLUMN-HDGS.
 		05 COLUMN-HEADINGS1    PIC X(8)  “MONTHLY ”.
 		05 COLUMN-HEADINGS2    PIC X(12) “TRANSACTIONS”. 
		05 COLUMN-HEADINGS3    PIC X(10) “ FOR APRIL”.
COBOL 2008 Changes
  1. Margins A and B rules will be guidelines rather than requirements
  2. PROGRAM-ID will be only paragraph in IDENTIFICATION DIVISION. All others can be specified as comments
  3. Length of user-defined words will be increased from 30 to 60 characters
  4. VALUE clause will be allowed in FILE SECTION
  5. Commas and dollar signs will be permissible in numeric literals

PROCEDURE DIVISION

  • Describes the algorithm which will process and produce the data previously described
  • Hierarchial in structure and consists of SECTIONS, PARAGRAPHS, SENTENCES, and STATEMENTS
  • Only the SECTION is optional. There must be at least one PARAGRAPH, SENTENCE and STATEMENT in the PROCEDURE DIVISION

SECTION

  • A SECTION is made up of one or more PARAGRAPHS
  • A section begins with the SECTION name and ends where the next SECTION name is encountered or where the program text ends
  • A section name is the PROCEDURE DIVISION is a name chosen by the programmer followed by the word SECTION followed by a full stop

PARAGRAPHS

  • Each paragraph is an independent module or routine
  • A paragraph begins with the paragraph name and ends with the next paragraph or section name or the end of the program text
  • The paragraph name in PROCEDURE DIVISION is a name chosen by the programmer followed by a full stop
  • Paragraph-names
    • Follow rules for forming data-names except may be all digits: 1010, 1020, etc. are valid paragraph names
    • Must be unique
    • Coded in Area A, followed by perido

Sentences and Statements

  • A paragraph is made up of one or more sentences that perform a specific set of operations on the data
  • A sentences consists of one or more statements and is terminated by a full stop
MOVE .21 to VatRate
COMPUTE VatAmount = ProductCost * VatRate.

DISPLAY "Enter name " WITH NO ADVANCING
ACCEPT StudentName
DISPLAY "Name entered was " StudentName.
  • A statement consists of a COBOL verb and operand(s)
    • SUBTRACT Tax FROM GrossPay GIVING NetPay
    • MULTIPLY HOURS BY RATE GIVING WAGES

Example

IDENTIFICATION DIVISION.
PROGRAM-ID.  MultiplyProgram.


DATA DIVISION.
WORKING-STORAGE SECTION.
01  Num1           PIC 9  VALUE ZEROS.
01  Num2           PIC 9  VALUE ZEROS.
01  Result         PIC 99 VALUE ZEROS.

PROCEDURE DIVISION.
CalculateResult.
    ACCEPT Num1
    ACCEPT Num2
    MULTIPLY Num1 BY Num2 GIVING Result
    DISPLAY "Result is = ", Result
    STOP RUN.

COP 2121 Some Basic Cobol Verbs