Cobol Intro
Cobol Intro
COBOL
COBOL
COBOL is an acronym which stands for
Common Business Oriented Language.
The name indicates the target area of COBOL
applications.
COBOL is used for developing business, typically
file-oriented, applications.
It is not designed for writing systems programs. You
would not develop an operating system or a
compiler using COBOL.
COBOL is one of the oldest computer languages in
use (it was developed around the end of the 1950s).
As a result it has some idiosyncracies which
programmers may find irritating.
COBOL idiosyncracies
One of the design goals was to make the language as
English-like as possible. As a consequence
the COBOL reserved word list is quite extensive and
contains hundreds of entries.
COBOL uses structural concepts normally associated
with English prose such as section, paragraph, sentence
and so on.
As a result COBOL programs tend to be verbose.
Some implementations require the program text to
adhere to certain, archaic, formatting restrictions.
Although modern COBOL has introduced many of the
constructs required to write well structured programs it
also still retains elements which, if used, make it
difficult, and in some cases impossible, to write good
programs.
The COBOL Metalanguage - Syntax Notation
Words in uppercase are reserved words.
When underlined they must be present when the
operation of which they are a part is used.
When they are not underlined the used for readability
only and are optional. If used they must be spelt correctly.
Words in mixed case represent names which will be
devised by the programmer.
When material is enclosed in braces { } a choice must
be made from the options within the braces.
Material is enclosed in square brackets [ ] indicates that
the material is an option which may be included or
omitted as required.
The ellipsis symbol ‘...’ indicates that the preceding
syntax element may be repeated at the programmer’s
discretion.
Structure of COBOL programs.
Program
Divisions
Section(s)
Paragraph(s)
Sentence(s)
Statement(s)
The Four Divisions.
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
IDENTIFICATION DIVISION.
Program Details
NOTE
DATA DIVISION. The keyword
DIVISION and a
‘full-stop’ is used
Data Descriptions in every case.
PROCEDURE DIVISION.
Algorithm Description
IDENTIFICATION DIVISION.
WORKING-STORAGE SECTION.
DATA DIVISION.
FILE SECTION.
File Section entries.
WORKING-STORAGE SECTION.
WS entries.
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. Sequence-Program.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE ZEROS.
01 Num2 PIC 9 VALUE ZEROS.
01 Result PIC 99 VALUE ZEROS.
The PROCEDURE DIVISION.
FILE SECTION.
Paragraphs
PROGRAM-ID.
Sentences and Statements.
A paragraph consists of one or more sentences.
A sentence 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 an operand
or operands.
SUBTRACT Tax FROM GrossPay GIVING NetPay
READ StudentFile
AT END SET EndOfFile TO TRUE
END-READ
A Full COBOL program.
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. SequenceProgram.
AUTHOR. Michael Coughlan.
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.
The minimum COBOL program.
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. SmallestProgram.
PROCEDURE DIVISION.
DisplayPrompt.
DISPLAY "I did it".
STOP RUN.