Module 3: COBOL Program structure
COBOL- Program structure
COBOL Program Structure is represented in diagram shown below:-
Explanation for above given Structure is as follows:-
- Every COBOL Source program is made up of 4 divisions. No code can be written outside these divisions.
- A division may contain one or more sections or paragraphs.
- The names of these divisions are predefined by language and cannot be changed
- All these divisions must begin in Margin A and can be continued to Margin B of Coding Sheet
- Each division can be started by specifying division name and it ends at the beginning of the next division or at the end of program
- There are 4 divisions in COBOL
- IDENTIFICATION DIVISION :- This division is used to identify program to operating system and also provides documentation details about the program. It is explained in detail here
- ENVIRONMENT DIVISION :- This division is used to define input/output file names and to describe devices used to store them. It is explained in detail here
- DATA DIVISION :- This division is used to describe input and output format of data in files. Also, this can be used to define any constants and work areas. It is explained in detail here
- PROCEDURE DIVISION :- This is the division, where programmer writes program logic. It is explained in detail here
- A section may contain one or more paragraphs
- Few section are pre-defined by language. We can also code user-defined section in Procedure Division.
- All section must begin in Margin A and can be continued to Margin B of Coding Sheet
- Section begin with the section name and ends at the beginning of another section or division; If section is specified in PROCEDURE DIVISION, it can end at the end of PROCEDURE DIVISION or at the key words END DECLARATIVES in the declaration portion of the PROCEDURE DIVISION
FILE SECTION. <-- Pre-defined section <file-entries> EMPLOYEE SECTION. <-- user-defined section <user-defined statement>
- A paragraph may contain one or more statements terminated by period (.)
- Few paragraphs are pre-defined by language. We can also code user-defined paragraphs in PROCEDURE DIVISION
- All paragraphs must begin in Margin A and can be continued to Margin B of coding Sheet
- Paragraphs begin with paragraph name and ends at the beginning of another paragraph, section, division; It also ends at the end of program. There are few other statements also available to end paragraphs which will be explained in later modules
PROGRAM-ID. <-- Example of Pre-defined paragraph 1000-EMP-PARA. <-- Example of User-defined paragraph
- A sentence may contain one or more statements terminated by period (.)
- All sentences must be coded within Margin B of Coding Sheet
- Example of Sentence:-MOVE DATA-A TO DATA-B MOVE DATA-B TO DATA-C MOVE DATA-C TO DATA-A.
- In above example, we coded three statements and terminated last statement with period sign (.). Thus, these three lines of code can be considered as single sentence
- A statement may consists of one or more valid words and verbs
- Generally, statement has a COBOL Verb followed by operand or operands. Statement can be considered as stand-alone instruction
- All statements must be coded within Margin B of codding sheet
- There are 4 types of statements in COBOL language:-
- Imperative statements :- Imperative statements are those which indicate the specific unconditional action to be performed by the object program i.e. these statements are not having any conditions to execute. For example: - ‘MOVE A TO B’
- Compiler directive statements :- As the name suggests, the Compiler Directive statements direct the COBOL compiler and no corresponding statements are generated for this in the load module. ‘COPY’ and ‘USE’ statements are examples of Compiler directive statement
- Conditional statements :- A conditional statement specifies that the true or false value of the condition is to be determined. If the condition is true, the statement following condition are executed. If the condition is false, the subsequent statements dependent on true value of condition will be skipped. ‘IF’ and ‘EVALUATE’ statements are examples of conditional statements
- Delimited scope statements :- When a conditional statement(for example, IF statement) is ended with an explicit end verb scope terminator (for example, END-IF), the statement is called as Delimited scope statement
COBOL words, verbs
- A COBOL word or verb comprise of one or more characters. They can be user-defined or pre-defined by language. For example ‘MOVE’, ‘ACCEPT’ are verb
- Characters are lowest element in the COBOL program structure and cannot be divided further.
- It may be Digits (0-9), Alphabets (A-Z), Space (b), Special characters (+ - * / ( ) = $ ; “ > < , .) etc. Complete list of character described in ‘Character Set’ article here