COBOL Foundation
BENSON-IYARE, Jessica C.
Structure/Components of COBOL programs
O The organization of a COBOL program is
hierarchical.
O It's not necessarily needed for all of the
components to be present for the hierarchical
relationship to exist.
O COBOL Programming structure. Here, are various
components of the COBOL program:
2
components of the COBOL program-Divisions
O 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.
O Each DIVISION of a COBOL program is broken
down into SECTIONs, beginning in special lines
called section header.
O Section headers consist of the name of the section
followed by the word “SECTION” and a period.
O Section names are devised by the programmer, or
defined by the language.
O A section name is followed by the word SECTION
and a period.
3
3
components of the COBOL program-Sections
O Sections are the logical subdivision of the
program logic. It is the collection of paragraphs.
O 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.
O Paragraphs in COBOL may be part of a
SECTION or can stand alone within their
DIVISION.
O In all cases, paragraphs are identified by a
paragraph header, which consists solely of the
name of the paragraph followed by a period.
4
Components of the COBOL program-Paragraphs
O Example . PROCEDURE DIVISION with Two
Paragraphs (Begin and DisplayGreeting)
PROCEDURE DIVISION.
Begin.
PERFORM DisplayGreeting 10 TIMES.
STOP RUN.
DisplayGreeting.
DISPLAY "Greetings from COBOL".
5
Components of the COBOL program-Paragraphs
cont’d
O Each SECTION of a COBOL program is broken
down into paragraphs.
O 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.
O A paragraph name is devised by the
programmer or defined by the language, and
is followed by a period.
See the two example names below:
PrintFinalTotals. (devised by the
programmer)
PROGRAM-ID. (devised by the language)
6
components of the COBOL program-
Sentences
O A sentence consists of one or more statements
and is terminated by a period (.).
O There must be at least one sentence, and hence
one period, in a paragraph.
O The example below shows two sentences. The
first sentence also happens to be a statement;
the second consists of three statements.
O Example Two Sentences
SUBTRACT Tax FROM GrossPay GIVING NetPay.
MOVE .21 TO VatRate
COMPUTE VatAmount = ProductCost * VatRate
DISPLAY "The VAT amount is - " VatAmount. 7
components of the COBOL program-
Statements
O In COBOL, language statements are referred to
as verbs. A statement starts with the name of
the verb and is followed by the operand or
operands on which the verb acts.
O Example Three Statements
DISPLAY "Enter name " WITH NO ADVANCING
ACCEPT StudentName
DISPLAY "Name entered was " StudentName
8
The major COBOL verbs are categorized by type
O In table in the next slide, the major COBOL
verbs are categorized by type.
O The arithmetic verbs are used in computations,
O The file-handling verbs are used to manipulate
files,
O The flow-of-control verbs are used to alter the
normal sequential execution of program
statements,
O The table-handling verbs are used to manipulate
tables (arrays), and
O The string-handling verbs allow such operations
as character counting, string splitting, and string
concatenation.
9
Table Major COBOL verbs are categorized by type
10
The COBOL DIVISIONs
O As earlier said, a division is a block of code,
which usually contains one or more sections.
O It starts where the division name is encountered.
O It ends with the beginning of the next division or
with the end of the program text.
O There are four divisions:
1.The IDENTIFICATION DIVISION-Contains
information about the program
2.The ENVIRONMENT DIVISION-Contains
environment information
3.The DATA DIVISION-Contains data descriptions;
and
4.The PROCEDURE DIVISION-Contains the
program algorithms.
11
IDENTIFICATION DIVISION
O The IDENTIFICATION DIVISION supplies
information about the program to the
programmer and the compiler.
O The identification division consists only of
paragraphs (no SECTIONs).
IDENTIFICATION DIVISION.
PROGRAM-ID. IdentificationDivision.
[AUTHOR. Ifesinachi.]
[DATE-WRITTEN. 10th March 2020.]
[DATE-COMPILED. 10th March 2020.]
…
12
IDENTIFICATION DIVISION cont’d
O The PROGRAM-ID paragraph is the only entry
required. In fact, this entry is required in every
program.
O Nowadays all the other entries have the status of
comments (which are not processed when the
program runs), but you may still find it useful to
include paragraphs such as AUTHOR and DATE-
WRITTEN.
O The PROGRAM-ID is followed by a user-devised
name that is used to identify the program
internally.
O This name may be different from the file name
given to the program when it was saved to
backing storage. 13
IDENTIFICATION DIVISION Cont’d
O The example below shows an example of
IDENTIFICATION DIVISION.
O Pay particular attention to the periods — they
are required.
O Example Sample IDENTIFICATION
DIVISION
IDENTIFICATION DIVISION.
PROGRAM-ID. PrintSummaryReport.
AUTHOR. Ifechukwudeni.
DATE-WRITTEN. 20th Feb 2020.
14
ENVIRONMENT DIVISION
O Environment division is used to describe the
environment in which the program works.
O It consists of two sections:
O 1.) Configuration section 2.) Input-Output
section
1. Configuration section provides information
about the system on which the program is written
and executed.
It consists of three paragraphs:
O a. Source-Computer
O b. Object-Computer
O c. Special-Name
15
ENVIRONMENT DIVISION Cont’d
O SOURCE-COMPUTER: System used to
compile the program. (i.e. describes the
computer used to compile the source
program).
O OBJECT-COMPUTER: System used to
execute the program. (i.e. describes the
computer used to run the source program).
O SPECIAL-NAME: Allows you to specify such
environmental details as what alphabet to use,
what currency symbol to use, and what
decimal point symbol to use.
16
ENVIRONMENT DIVISION cont’d
2. Input-Output section provides information about
the files to be used in the program.
Structurally, Input-Output section consists of ONE
paragraph:
O FILE-CONTROL paragraph: Provides information of
external data sets used in the program.(i.e. lets you
connect internal file names with external devices and
files).
O The FILE-CONTROL paragraph must contain a
SELECT statement for every file (input, output, or
input-output) to be processed by the program.
O Each SELECT statement consists of several
clauses and ends with a period (.).
The syntax is:
SELECT file-name
ASSIGN TO external-name.
17
ENVIRONMENT DIVISION cont’d
O Consider the program segment below:
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. XXX-ZOS.
OBJECT-COMPUTER. XXX-ZOS.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEN
ASSIGN TO DDNAME.
ORGANIZATION IS
SEQUENTIAL.
18
DATA DIVISION
O The DATA DIVISION is used to describe most of
the data that a program processes.
O The obvious exception to this is literal data, which
is defined in situ as a string or numeric literal such
as “Freddy Ryan” or -345.74.
The DATA DIVISION is divided into four
sections:
• The FILE SECTION
• The WORKING-STORAGE SECTION
• The LINKAGE SECTION
• The REPORT SECTION
O The first two are the main sections. The LINKAGE
SECTION is used only in subprograms, and the
REPORT SECTION is used only when generating
reports. 19
DATA DIVISION Cont’d
O The FILE SECTION describes the data that is
sent to, or comes from, the computer’s data
storage peripherals.
O These include such devices as card readers,
magnetic tape drives, hard disks, CDs, and
DVDs.
O The WORKING-STORAGE SECTION
describes the general variables used in the
program.
20
COBOL Program showing the DATA Division
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEN
ASSIGN TO INPUT.
ORGANIZATION IS SEQUENTIAL.
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILEN
01 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT PIC A(30).
01 WS-ID PIC 9(5).
LOCAL-STORAGE SECTION.
01 LS-CLASS PIC 9(3).
LINKAGE SECTION.
01 LS-ID PIC 9(5).
PROCEDURE DIVISION.
DISPLAY 'Executing COBOL program using JCL'.
STOP RUN.
21
COBOL Program-Another Example
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEN
ASSIGN TO INPUT.
ORGANIZATION IS SEQUENTIAL.
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILEN
01 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT PIC A(30).
01 WS-ID PIC 9(5).
PROCEDURE DIVISION.
DISPLAY 'Executing COBOL program using JCL'.
STOP RUN.
What will be the output of this program? 22
Simple Data Declarations
IDENTIFICATION DIVISION.
PROGRAM-ID. SimpleDataDeclarations.
AUTHOR. Ifechukwudeni.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CardinalNumber PIC 99 VALUE ZEROS.
01 IntegerNumer PIC S99 VALUE -14.
01 DecimalNumber PIC 999V99 VALUE 543.21.
01 ShopName PIC X(30) VALUE SPACES.
01 ReportHeading PIC X(25) VALUE "==
Employment Report ==".
23
DATA HIERARCHY
O All the data items in the simple data declarations
example are independent, elementary, items.
Although data hierarchy is too complicated a topic
to deal with at this point, a preview of hierarchical
data declaration is given in BirthDate (see below).
O Example of a Hierarchical Data Declaration
01 BirthDate.
02 YearOfBirth.
03 CenturyOB PIC 99.
03 YearOB PIC 99.
02 MonthOfBirth PIC 99.
02 DayOfBirth PIC 99.
24
PROCEDURE DIVISION
O Procedure division is used to include the logic of
the program. It consists of executable statements
using variables defined in the data division.
O In this division, paragraph and section names are
userdefined. The names chosen should reflect
the function of the code contained in the
paragraph or section.
O Only the section is optional.
O There must be at least one statement in the
procedure division.
O The last statement to end the execution in this
division is either STOP RUN which is used in the
calling programs or EXIT PROGRAM which is
used in the called programs.
25
PROCEDURE DIVISION cont’d
O The PROCEDURE DIVISION contains the code
used to manipulate the data described in the
DATA DIVISION. It is here that the programmer
describes his/her algorithm.
Example: Hello World Program
IDENTIFICATION DIVISION.
PROGRAM-ID. Hello World Program.
PROCEDURE DIVISION.
SayHello.
DISPLAY "Hello world".
STOP RUN.
26

COBOL Foundation 1

  • 1.
  • 2.
    Structure/Components of COBOLprograms O The organization of a COBOL program is hierarchical. O It's not necessarily needed for all of the components to be present for the hierarchical relationship to exist. O COBOL Programming structure. Here, are various components of the COBOL program: 2
  • 3.
    components of theCOBOL program-Divisions O 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. O Each DIVISION of a COBOL program is broken down into SECTIONs, beginning in special lines called section header. O Section headers consist of the name of the section followed by the word “SECTION” and a period. O Section names are devised by the programmer, or defined by the language. O A section name is followed by the word SECTION and a period. 3 3
  • 4.
    components of theCOBOL program-Sections O Sections are the logical subdivision of the program logic. It is the collection of paragraphs. O 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. O Paragraphs in COBOL may be part of a SECTION or can stand alone within their DIVISION. O In all cases, paragraphs are identified by a paragraph header, which consists solely of the name of the paragraph followed by a period. 4
  • 5.
    Components of theCOBOL program-Paragraphs O Example . PROCEDURE DIVISION with Two Paragraphs (Begin and DisplayGreeting) PROCEDURE DIVISION. Begin. PERFORM DisplayGreeting 10 TIMES. STOP RUN. DisplayGreeting. DISPLAY "Greetings from COBOL". 5
  • 6.
    Components of theCOBOL program-Paragraphs cont’d O Each SECTION of a COBOL program is broken down into paragraphs. O 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. O A paragraph name is devised by the programmer or defined by the language, and is followed by a period. See the two example names below: PrintFinalTotals. (devised by the programmer) PROGRAM-ID. (devised by the language) 6
  • 7.
    components of theCOBOL program- Sentences O A sentence consists of one or more statements and is terminated by a period (.). O There must be at least one sentence, and hence one period, in a paragraph. O The example below shows two sentences. The first sentence also happens to be a statement; the second consists of three statements. O Example Two Sentences SUBTRACT Tax FROM GrossPay GIVING NetPay. MOVE .21 TO VatRate COMPUTE VatAmount = ProductCost * VatRate DISPLAY "The VAT amount is - " VatAmount. 7
  • 8.
    components of theCOBOL program- Statements O In COBOL, language statements are referred to as verbs. A statement starts with the name of the verb and is followed by the operand or operands on which the verb acts. O Example Three Statements DISPLAY "Enter name " WITH NO ADVANCING ACCEPT StudentName DISPLAY "Name entered was " StudentName 8
  • 9.
    The major COBOLverbs are categorized by type O In table in the next slide, the major COBOL verbs are categorized by type. O The arithmetic verbs are used in computations, O The file-handling verbs are used to manipulate files, O The flow-of-control verbs are used to alter the normal sequential execution of program statements, O The table-handling verbs are used to manipulate tables (arrays), and O The string-handling verbs allow such operations as character counting, string splitting, and string concatenation. 9
  • 10.
    Table Major COBOLverbs are categorized by type 10
  • 11.
    The COBOL DIVISIONs OAs earlier said, a division is a block of code, which usually contains one or more sections. O It starts where the division name is encountered. O It ends with the beginning of the next division or with the end of the program text. O There are four divisions: 1.The IDENTIFICATION DIVISION-Contains information about the program 2.The ENVIRONMENT DIVISION-Contains environment information 3.The DATA DIVISION-Contains data descriptions; and 4.The PROCEDURE DIVISION-Contains the program algorithms. 11
  • 12.
    IDENTIFICATION DIVISION O TheIDENTIFICATION DIVISION supplies information about the program to the programmer and the compiler. O The identification division consists only of paragraphs (no SECTIONs). IDENTIFICATION DIVISION. PROGRAM-ID. IdentificationDivision. [AUTHOR. Ifesinachi.] [DATE-WRITTEN. 10th March 2020.] [DATE-COMPILED. 10th March 2020.] … 12
  • 13.
    IDENTIFICATION DIVISION cont’d OThe PROGRAM-ID paragraph is the only entry required. In fact, this entry is required in every program. O Nowadays all the other entries have the status of comments (which are not processed when the program runs), but you may still find it useful to include paragraphs such as AUTHOR and DATE- WRITTEN. O The PROGRAM-ID is followed by a user-devised name that is used to identify the program internally. O This name may be different from the file name given to the program when it was saved to backing storage. 13
  • 14.
    IDENTIFICATION DIVISION Cont’d OThe example below shows an example of IDENTIFICATION DIVISION. O Pay particular attention to the periods — they are required. O Example Sample IDENTIFICATION DIVISION IDENTIFICATION DIVISION. PROGRAM-ID. PrintSummaryReport. AUTHOR. Ifechukwudeni. DATE-WRITTEN. 20th Feb 2020. 14
  • 15.
    ENVIRONMENT DIVISION O Environmentdivision is used to describe the environment in which the program works. O It consists of two sections: O 1.) Configuration section 2.) Input-Output section 1. Configuration section provides information about the system on which the program is written and executed. It consists of three paragraphs: O a. Source-Computer O b. Object-Computer O c. Special-Name 15
  • 16.
    ENVIRONMENT DIVISION Cont’d OSOURCE-COMPUTER: System used to compile the program. (i.e. describes the computer used to compile the source program). O OBJECT-COMPUTER: System used to execute the program. (i.e. describes the computer used to run the source program). O SPECIAL-NAME: Allows you to specify such environmental details as what alphabet to use, what currency symbol to use, and what decimal point symbol to use. 16
  • 17.
    ENVIRONMENT DIVISION cont’d 2.Input-Output section provides information about the files to be used in the program. Structurally, Input-Output section consists of ONE paragraph: O FILE-CONTROL paragraph: Provides information of external data sets used in the program.(i.e. lets you connect internal file names with external devices and files). O The FILE-CONTROL paragraph must contain a SELECT statement for every file (input, output, or input-output) to be processed by the program. O Each SELECT statement consists of several clauses and ends with a period (.). The syntax is: SELECT file-name ASSIGN TO external-name. 17
  • 18.
    ENVIRONMENT DIVISION cont’d OConsider the program segment below: ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. XXX-ZOS. OBJECT-COMPUTER. XXX-ZOS. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILEN ASSIGN TO DDNAME. ORGANIZATION IS SEQUENTIAL. 18
  • 19.
    DATA DIVISION O TheDATA DIVISION is used to describe most of the data that a program processes. O The obvious exception to this is literal data, which is defined in situ as a string or numeric literal such as “Freddy Ryan” or -345.74. The DATA DIVISION is divided into four sections: • The FILE SECTION • The WORKING-STORAGE SECTION • The LINKAGE SECTION • The REPORT SECTION O The first two are the main sections. The LINKAGE SECTION is used only in subprograms, and the REPORT SECTION is used only when generating reports. 19
  • 20.
    DATA DIVISION Cont’d OThe FILE SECTION describes the data that is sent to, or comes from, the computer’s data storage peripherals. O These include such devices as card readers, magnetic tape drives, hard disks, CDs, and DVDs. O The WORKING-STORAGE SECTION describes the general variables used in the program. 20
  • 21.
    COBOL Program showingthe DATA Division IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILEN ASSIGN TO INPUT. ORGANIZATION IS SEQUENTIAL. ACCESS IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD FILEN 01 NAME PIC A(25). WORKING-STORAGE SECTION. 01 WS-STUDENT PIC A(30). 01 WS-ID PIC 9(5). LOCAL-STORAGE SECTION. 01 LS-CLASS PIC 9(3). LINKAGE SECTION. 01 LS-ID PIC 9(5). PROCEDURE DIVISION. DISPLAY 'Executing COBOL program using JCL'. STOP RUN. 21
  • 22.
    COBOL Program-Another Example IDENTIFICATIONDIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILEN ASSIGN TO INPUT. ORGANIZATION IS SEQUENTIAL. ACCESS IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD FILEN 01 NAME PIC A(25). WORKING-STORAGE SECTION. 01 WS-STUDENT PIC A(30). 01 WS-ID PIC 9(5). PROCEDURE DIVISION. DISPLAY 'Executing COBOL program using JCL'. STOP RUN. What will be the output of this program? 22
  • 23.
    Simple Data Declarations IDENTIFICATIONDIVISION. PROGRAM-ID. SimpleDataDeclarations. AUTHOR. Ifechukwudeni. DATA DIVISION. WORKING-STORAGE SECTION. 01 CardinalNumber PIC 99 VALUE ZEROS. 01 IntegerNumer PIC S99 VALUE -14. 01 DecimalNumber PIC 999V99 VALUE 543.21. 01 ShopName PIC X(30) VALUE SPACES. 01 ReportHeading PIC X(25) VALUE "== Employment Report ==". 23
  • 24.
    DATA HIERARCHY O Allthe data items in the simple data declarations example are independent, elementary, items. Although data hierarchy is too complicated a topic to deal with at this point, a preview of hierarchical data declaration is given in BirthDate (see below). O Example of a Hierarchical Data Declaration 01 BirthDate. 02 YearOfBirth. 03 CenturyOB PIC 99. 03 YearOB PIC 99. 02 MonthOfBirth PIC 99. 02 DayOfBirth PIC 99. 24
  • 25.
    PROCEDURE DIVISION O Proceduredivision is used to include the logic of the program. It consists of executable statements using variables defined in the data division. O In this division, paragraph and section names are userdefined. The names chosen should reflect the function of the code contained in the paragraph or section. O Only the section is optional. O There must be at least one statement in the procedure division. O The last statement to end the execution in this division is either STOP RUN which is used in the calling programs or EXIT PROGRAM which is used in the called programs. 25
  • 26.
    PROCEDURE DIVISION cont’d OThe PROCEDURE DIVISION contains the code used to manipulate the data described in the DATA DIVISION. It is here that the programmer describes his/her algorithm. Example: Hello World Program IDENTIFICATION DIVISION. PROGRAM-ID. Hello World Program. PROCEDURE DIVISION. SayHello. DISPLAY "Hello world". STOP RUN. 26

Editor's Notes

  • #5 Sections are the logical subdivision of the program logic. It is the collection of paragraphs.
  • #7 Paragraphs are the portion of a selection which is a user-defined or predefined name which should be followed by a period. This section includes zero or more sentences.
  • #8 Characters: These are the lowest in the hierarchy which cannot be divided. Sentences are a combination of single or multiple statements. They should occur only in the procedure division. A sentence in COBOL language consists of one or more statements and must be terminated by a period (.). For example: MOVE .21 TO VatRate MOVE 1235.76 TO ProductCost COMPUTE VatAmount = ProductCost * VatRate. Statements are the meaningful COBOL verbs which perform some processing.
  • #12 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. Types of Divisions Various divisions in COBOL program structure are as follows: Identification Division Environment Division Data Division Procedure Division
  • #14 Identification Division The programmer and compiler can use this division to recognize the program. The only compulsory division is Program ID. It identifies the name of the program which contains characters.
  • #16 Environment division is used to specify input and output files to the program. It consists of two sections: a. Configuration section provides information about the system on which the program is written and executed. It consists of two paragraphs: Source computer : System used to compile the program. Object computer : System used to execute the program.
  • #18 I-O control : Provides information of files used in the program.
  • #19 I-O control : Provides information of files used in the program.
  • #27 Shortest COBOL Program COBOL has a very bad reputation for verbosity, but most of the programs on which that reputation was built were written in ANS 68 or ANS 74 COBOL. Those programs are over 40 years old. In modern versions of the language, program elements are not required unless explicitly used. IDENTIFICATION DIVISION. PROGRAM-ID. ShortestProgram. PROCEDURE DIVISION. DisplayPrompt. DISPLAY “Vision 2020".