Introduction to COBOL



    About COBOL:

  1. COBOL is a high-level programming language first developed by the CODASYL Committee (Conference on Data Systems Languages) in 1960.

  2. Currently responsibility for developing new COBOL standards has been assumed by the American National Standards Institute (ANSI).

  3. Three ANSI standards for COBOL have been produced: in 1968, 1974 and 1985

  4. The word COBOL is an acronym that stands for COmmon Business OrientedLanguage

  5. COBOL is designed for developing business, typically file-oriented, applications. It is not designed for writing systems programs.

  6. Advantages of Using COBOL
    1. COBOL is self-documenting
    2. COBOL is simple
    COBOL is a simple language (no pointers, no user defined functions, no user defined types) with a limited scope of function.
    1. COBOL is non-proprietary (portable)
    The COBOL standard does not belong to any particular vendor. The vendor independent ANSI COBOL committee legislates formal, non-vendor-specific syntax and semantic language standards.
    1. COBOL is Maintainable
    COBOL has a 30 year proven track record for application maintenance, enhancement and production support at the enterprise level.

    What is a Program?
    A program is a collection of statements written in a language the computer understands.
    A computer executes program statements one after another in sequence until it reaches the end of the program unless some statement in the program alters the order of execution.
    Any program can be written using the three main programming constructs
    1. Sequence
    2. Selection
    3. Iteration

    What is COBOL MetaLanguage.
  7. It’s the language used to explain the syntax of cobol verbs etc .

  8. As per this notation
    1. Words in uppercase are reserved words.
    2. When underlined they are mandatory.
    3. When not underlined they are "noise" words, used for readability only, and are optional.
    4. Words in mixed case represent names that must be devised by the programmer (like data item names).
    5. When material is enclosed in curly braces { }, a choice must be made from the options within the braces. If there is only one option then that item in mandatory.
    6. Material enclosed in square brackets [ ], indicates that the material is optional, and may be included or omitted as required.
    7. The ellipsis symbol ... (three dots), indicates that the preceding syntax element may be repeated at the programmer's discretion.

    Apart from the above some more notations are used:

    $i
    uses an alphanumeric data-item
    $il
    uses an alphanumeric data-item or a string literal
    #i
    uses a numeric data-item
    #il
    uses a numeric data-item or numeric literal
    $#i
    uses a numeric or an alphanumeric data-item

  9. Example:



  10. This syntax diagram may be interpreted as follows;
    1. We must start a COMPUTE statement with the keyword COMPUTE.
    2. We must follow the keyword with the name(s) of the numeric data item (or items - note the ellipsis symbol (...)) to be used to receive the result of the expression.
    3. The #i suffix at the end of word Result tells us that a numeric identifier/data item must be used.
    4. Since the ellipsis symbol is placed outside the curly brackets we can interpret this to mean that each result field can have its own ROUNDED phase. In other words we could have a COMPUTE statement like -

    COMPUTE Result1 ROUNDED, Result2 = ((9*9)+8)/5

    1. The square brackets after the Arithmetic Expression indicate that the next items are optional but if used we must choose between the ON SIZE ERROR or NOT ON SIZE ERROR phrases.
    2. Because the END-COMPUTE is contained within the square brackets it must only be used when a SIZE ERROR or NOT SIZE ERROR phrase is used.


    COBOL coding rules

  11. The first six character positions are reserved for sequence numbers.

  12. The seventh character position is reserved for the continuation character, or for an asterisk that denotes a comment line.

  13. The actual program text starts in column 8.

  14. The four positions from 8 to 11 are known as Area A, and positions from 12 to 72 are Area B.

  15. When a COBOL compiler recognizes the two areas, all division names, section names, paragraph names, FD entries and 01 level numbers must start in Area A. All other sentences must start in Area B.


  16. User defined Name Construction:

    All user-defined names, such as data names, paragraph names, section names condition names and mnemonic names, must adhere to the following rules:
    1. They must contain at least one character, but not more than 30 characters.

    1. They must contain at least one alphabetic character.

    1. They must not begin or end with a hyphen.

    1. They must be constructed from the characters A to Z, the numbers 0 to 9, and the hyphen.

    1. They must not contain spaces.

    1. Names are not case-sensitive: TotalPay is the same as totalpay, Totalpay orTOTALPAY.

    The structure of COBOL programs

  17. COBOL programs are hierarchical in structure.

  18. The hierarchy consists of Divisions, Sections, Paragraphs, Sentences and Statements.

  19. A Division may contain one or more Sections, a Section one or more Paragraphs, a Paragraph one or more Sentences and a Sentence one or more Statements

  20. Divisions
  21. A division is a block of code, usually containing one or more sections, that starts where the division name is encountered and ends with the beginning of the next division or with the end of the program text.
  22. Sections
  23. A section is a block of code usually containing 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.
    Section names are devised by the programmer, or defined by the language. A section name is followed by the word SECTION and a period.
  24. Paragraphs
  25. A paragraph is a block of code made up of one or more sentences.
    A paragraph begins with the paragraph name and ends with the next paragraph or section name or the end of the program text.
    A paragraph name is devised by the programmer or defined by the language, and is followed by a period. 
  26. Sentences and statements
  27. A sentence consists of one or more statements and is terminated by a period.

    MOVE .21 TO VatRate
       MOVE 1235.76 TO ProductCost
       COMPUTE VatAmount = ProductCost * VatRate.

    A statement consists of a COBOL verb and an operand or operands. 
    For example:
    SUBTRACT Tax FROM GrossPay GIVING NetPay


    Different Divisions in COBOL program:

  28. At the top of the COBOL hierarchy are the four divisions.

  29. Although some of the divisions may be omitted, the sequence in which they are specified is fixed, and must follow the order below.

    1. IDENTIFICATION DIVISION. Contains program information
    2. ENVIRONMENT DIVISION. Contains environment information
    3. DATA DIVISION. Contains data descriptions
    4. PROCEDURE DIVISION. Contains the program algorithms

  30. The IDENTIFICATION DIVISION
    1. The IDENTIFICATION DIVISION supplies information about the program to the programmer and the compiler.
    2. Most entries in the IDENTIFICATION DIVISION are directed at the programmer. The compiler treats them as comments.
    The PROGRAM-ID clause is an exception to this rule. Every COBOL program must have a PROGRAM-ID because the name specified after this clause is used by the linker when linking a number of subprograms into one run unit, and by the CALL statement when transferring control to a subprogram.
    1. The IDENTIFICATION DIVISION has the following structure:
    IDENTIFICATION DIVISION
    PROGRAM-ID. NameOfProgram.
    [AUTHOR. YourName.]
    other entries here
    1. PROGRAM-ID is a paragraph name that must be specified immediately after the division header.
    2. NameOfProgram is a name devised by the programmer, and must satisfy the rules for user-defined names.

  31. The ENVIRONMENT DIVISION
    1. The ENVIRONMENT DIVISION is used to describe the environment in which the program will run.
    2. The purpose of the ENVIRONMENT DIVISION is to isolate in one place all aspects of the program that are dependant upon a specific computer, device or encoding sequence.The idea behind this is to make it easy to change the program when it has to run on a different computer or one with different peripheral devices.

  32. The DATA DIVISION
    1. As the name suggests, the DATA DIVISION provides descriptions of the data-items processed by the program.
    2. The DATA DIVISION has two main sections: the FILE SECTION and the WORKING-STORAGE SECTION.
    3. Additional sections, such as the LINKAGE SECTION (used in subprograms) and the REPORT SECTION (used in Report Writer based programs) may also be required.
    4. The FILE SECTION is used to describe most of the data that is sent to, or comes from, the computer's peripherals.
    5. The DATA DIVISION has the following structure and syntax:


  33. The PROCEDURE DIVISION
    1. The PROCEDURE DIVISION contains the code used to manipulate the data described in the DATA DIVISION
    2. The PROCEDURE DIVISION is hierarchical in structure and consists of sections, paragraphs, sentences and statements.
    3. Only the section is optional. There must be at least one paragraph, sentence and statement in the PROCEDURE DIVISION.
    4. Paragraph and section names in the PROCEDURE DIVISION are chosen by the programmer and must conform to the rules for user-defined names.
    5. Some COBOL compilers require that all the divisions be present in a program while others only require the IDENTIFICATION DIVISION and the PROCEDURE DIVISION



    Example:


    Example Program

    *------------------------------------------------
           IDENTIFICATION                         DIVISION.
          *------------------------------------------------
           PROGRAM-ID.  SequenceProgram.
          *------------------------------------------------
          * Purpose:                 To display the product of two values
          * Description:        Read 2 input values from the keyboard, compute                        
          *                   the product and display the result.
          * Calling statement: None.
          * Arguments:        None.
          * Input files:        None.
          * Output files:        None.
          * Programmer        Sukul Mahadik.
          * Supervisor:        None.
          * Date Written:        17, February 2012.
          *------------------------------------------------------
           DATA                                 DIVISION.
          *------------------------------------------------------
           WORKING-STORAGE                         SECTION.
          *------------------------------------------------------
          *         WSXX-Miscellneous.
          *------------------------------------------------------
           01  WS01-NUM1                           PIC 9.
           01  WS02-NUM2                           PIC 9.
           01  WS03-RESULT                         PIC 99.
       *------------------------------------------------
    PROCEDURE                         DIVISION.
    *------------------------------------------------
    A-MAIN                                SECTION.(section optional. Note that the name ends with a period)
    *------------------------------------------------
    A00.(paragraph name. Atleast one paragraph is necessary. Ends with a paragraph)
    ACCEPT WS01-NUM1
    ACCEPT WS02-NUM2
    MULTIPLY WS01-NUM1 BY WS02-NUM2 GIVING WS03-RESULT
    DISPLAY ‘Result is = ‘, WS03-RESULT
    STOP RUN.

.

No comments:

Post a Comment