The COBOL-85 Tutorial, Chapter 1
General introduction to the language and the compiler.
Copyright © 1998-2017 Kim Kjærsulf.
History and purposes.
Strengths and weaknesses of COBOL.
An introduction to COBOL's syntax.
Format of source lines.
Basic structure of a COBOL source program.
CBL notation.
Detailed structure of a COBOL source program.
A description of programmer-defined sections and paragraphs.
COBOL and screens.
Exercises
[This chapter]
[TOC]
[Tutorial]
The name of this programming language, COBOL, is an acronym that is derived from the statement "COmmon Business Oriented Language".
The first proposal on this programming language was presented in april 1960, in a report made by CODASYL (Conference On Data System Languages). A lot of improvement was done during the sixties ending up with an ANSI-definition in 1974.
For a decade thousands of programmers wrote millions (billions ?) of lines of source code in this language. In 1985 ANSI released a new definition of the language, called COBOL-85.
This new version of the language introduced improvement in at least two areas:
- Features that increase structured programming capability
- Features that facilitate code clarity and simplicity
- Miscellaneous features that provide additional functions
Because the language has such a long history, it is filled with obsolete constructs. The largest single reason for this, is the fact that many tasks have been automated by modern operating systems.
[This chapter]
[TOC]
[Tutorial]
If we compare COBOL with languages such as C++ or Pascal, the standard comment is that COBOL is hopelessly out of date and completely impossible to work with. However, closer study of the three programming languages results in a somewhat subtler answer.
COBOL contains a great many more possibilities for editing output than the other languages. The same number can, of course, be achieved in, for example, in C++, but only by drawing up and using a series of library routines.
COBOL has three facilities that make it head and shoulders above C and Pascal :
- Index sequential files
- Report management (COBOL's Report Writer Control System)
- Conversion between data types.
COBOL has its weaknesses, of course:
- Handling text strings
- No pointer concept and no dynamic storage allocation
- Recursive call is not usually supported
- No support of OOP in the standard, most common COBOL
Programs in the three languages are not equally easy to port between various platforms. Due to the supporting library modules Pascal and C++ are usually more difficult to port. But it is of cause possible to write programs in Cobol, that are hard to port too.
[This chapter]
[TOC]
[Tutorial]
There are syntax rules for both the vertical and the horizontal structure of a COBOL-source code.
Format of source lines.
Basic structure of a COBOL source program.
CBL notation.
[This chapter]
[TOC]
[Tutorial]
A COBOL source program is built up of lines in a rigid format that dates back to the time when the punched card was the normal (the only!) medium for inputting data. If syntax errors are to be avoided it is vital to adhere to this format strictly.
Positions 1 - 6
Previously used for line numbers, but no longer serve any formal purpose. I have seen these positions used for simple version-dating, i.e. when inserting new lines into the program, each new line had the date of insertion in these 6 positions. From the programs maintenance log, it is possible to see how have made the change.
Position 7
Position 7 is used for marking line types. There are five important options:
"*" The line is a comment line.
Any characters to the right of position 7 is ignored by the compiler.
Exclusion of lines from a program can be done by simply placing a "*" in position 7.
In combination with a date of change in positions 1-6, all changes in the program are documented.
"-" The line is a continuation of the previous line.
This is normally only used when a text between inverted commas continues beyond position 72.
"/" If a compiler listing is being used a slash in position 7 will give an obligatory page break.
"D" The line is to be used as part of a debug operation.
Modern compilers have tools that make visual code inspection possible. F.x. "animator" in MicroFocus compilers.
"$" Directive to the compiler.
Positions 8 - 11
These positions are known collectively as Column A.
All division, sections and paragraph names are to be defined starting in position 8, though they can of course continue as far as position 72.
Positions 12 - 72
Known collectively as column B.
Used for writing statements.
Position 73 - end of line
Texts written here are treated as comments.
MicroFocus compilers support a special kind of syntax for comments, called an inline comment. A space followed by the two characters "*>" tells the compiler that the remainder of the line is for commentary purpose only.
[This chapter]
[TOC]
[Tutorial]
Every COBOL program is vertically subdivided into four divisions:
THE IDENTIFICATION DIVISION.
Can be used to provide a brief description of the program, but there is only one mandatory element:
PROGRAM-ID.
PROGRAM-ID is used to assign a name to the program for links between different programs.
THE ENVIRONMENT DIVISION.
Used to describe the program environment – primarily the name and organization of the associated data files.
THE DATA DIVISION.
Used to describe the data structures of data files, program variables, and the interface used when calling other programs.
THE PROCEDURE DIVISION.
The procedure division is used to write the statements themselves, i.e. the programs' algorithm.
A source file contains one or more blocks with the structure described above, i.e. one for each module. Note too, that a procedure division can have a number of entry points.
[This chapter]
[TOC]
[Tutorial]
All COBOL statements are to be concluded with a full stop (a period).
Even though COBOL is subject to ANSI, there are still many facilities that have been added on by the various compiler suppliers. It is therefore necessary to have access to the correct manuals in order to know the precise syntax required. However, some points are more obviously open to creativity than others.
Every COBOL manual starts with an explanation of how syntax definitions are to be read. Square brackets, vertical lines and ellipses are part of this notation [ ... ].
COBOL syntax is not recursive which means, among other things, that it is not possible to use function calls and arithmetic expressions in a parameter list. This is an obvious weakness compared to languages such as C++, but that's just the way it is. There is, however, obvious advantages in this:- a COBOL program is almost all ways better to read and understand, compared to a C++ program. This also means less programming errors.
The limited amount of standard routines, is counterbalanced by the relatively large number of different implementations of instructions in COBOL. For example, for calculations, the compiler itself takes care of conversions between the various data formats. The programmer do not have do deal with these trivial necessities, there is however, the possibility that a program will perform a vast number of conversions between data formats, that could have been avoided easy by moving a value to an appropriate field, just once. This is left for a thoroughly examination of the program.
[This chapter]
[TOC]
[Tutorial]
Each division contains a number of standard sections, each with their own special purpose. Some of these are optional. This review of these sections follows the order found in the general source program structure:
IDENTIFICATION DIVISION.
PROGRAM-ID. Program name.
AUTHOR. Programmer's name added as a comment.
...
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
CONSOLE IS CRT
DECIMAL-POINT IS COMMA.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file name ...... File control entry.
...
DATA DIVISION.
FILE SECTION.
FD file-name .... Record description.
WORKING-STORAGE SECTION.
Declaration of variables.
REPORT SECTION.
Description of a report.
LINKAGE SECTION.
Definition of CALL parameters.
PROCEDURE DIVISION
DECLARATIVES.
... Handling IO errors.
END DECLARATIVES.
Algorithm.
{section-name SECTION [segment-number].
{paragraph-name.
Sentence.
}}
[This chapter]
[TOC]
[Tutorial]
A programmer-defined section should be viewed as an independent element of a COBOL program. A section represents a partially closed area within a program. By "partially" is meant that a section can function as a separate entity, but with full access to the program variables.
A paragraph is a named part of a section. A paragraph can therefore function independently, but can only be called from the section to which it belongs.
Sections and paragraphs are not the equivalent of a function in C++ or Pascal. Neither sections nor paragraphs can have their own local variables, nor can they be called using parameters. They have to access the global variables of the program of which they are a part.
A segment number is used to define whether a segment is to be resident or whether it is to be loaded as required (overlayable).
See also : Paragraph and section.
This example shows a programmer defined section (main) and two paragraphs (open-file, read-record).
* ------ executable instructions.
main section.
perform open-file.
perform read-record.
perform process
until eof.
perform close-file.
stop run.
*
*
open-file.
open input isamfile,
output rep-file.
initiate report.
*
read-record.
read isamfile next
at end move 1 to eof-code.
[This chapter]
[TOC]
[Tutorial]
COBOL has two instructions for managing input (ACCEPT) and output (DISPLAY) via the screen, and they are available in innumerable variations. Normally, however, these instructions are not used at all, and these operations are instead controlled by library routines working with a screen generator. A more detailed description of these operations falls outside the scope of this textbook.
This example shows some simple variations of accept and display.
The instructions begins by clearing the screen. After that the system date is copied into the variable "today". At the end the program prompts twice for the user to enter a figure, which is then read from the keyboard.
000000* A sample program showing the use of ACCEPT and DISPLAY
identification division.
* ------------ identifying the program.
program-id. accdisp.
author. kik.
environment division.
* ------------ program environment
configuration section.
special-names.
console is crt
decimal-point is comma.
* ------------ Variables
data division.
working-storage section.
77 ind-1 pic 9999.
77 ind-2 pic 9999.
01 todayis.
05 filler1 pic x(10) value "Today is: ".
05 today pic x(8).
procedure division.
* ------------ The instructions
main section.
display space.
accept today from date.
display todayis line 3 position 10.
display "Enter 1st figure :" line 5 position 10.
accept ind-1 line 5 position 27.
display "Enter 2nd figure :" line 6 position 10.
accept ind-2 line 6 position 27.
exit program.
[This chapter]
[TOC]
[Tutorial]
I have chosen this exercise to provide an example of how to enter and compile COBOL programs. There is a tradition that an introduction to a new programming language should start with a program example which merely prints the text "Hello world" on the screen.
Enter and compile this small COBOL program.
Model answer for this exercise.
identification division.
* ------ program identification.
program-id. hello.
author. kik.
environment division.
* ------ program environment.
configuration section.
special-names.
console is crt.
data division.
procedure division.
* ------ executable instructions.
main section.
display space.
display "Hello world" line 5 column 30.
stop run.
Notice that in the the line:
display "Hello world" line 5 column 30.
you might have to substitute the word "column" by "position".
Copyright © 1998-2017 Kim Kjærsulf
Last Updated october 3, 2003
For more information contact: Kim Kjærsulf