COP 2121 Basic Cobol Program Structure

From In The Wings
Revision as of 16:38, 22 February 2007 by Jka (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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