This tutorial introduces COBOL to you, in the form of a program that produces the Fibonacci sequence (I have got bored with "Hello World" tutorials ).
I have used the OpenCobol compiler on Linux.
So, without further ado, let's get started.
Divisions and Sections
COBOL is split into four divisions, the IDENTIFICATION DIVISION, the ENVIRONMENT DIVISION, the DATA DIVISION and the PROCEDURE DIVISION.
The IDENTIFICATION DIVISION is used to declare the program identification and is the only division that cannot be further sub-divided into SECTION's. Even the program identification is somewhat redundant, but still needs to be specified to successfully compile a program.
The ENVIRONMENT DIVISION is used to provide information pertaining to the environment in which the program will run and consists of two sections, the CONFIGURATION SECTION and the INPUT-OUTPUT SECTION. You will notice that in the code below, neither of these sections is specified as there is no need for them within the context of this program.
The DATA DIVISION is where we specify data items. This division consists of six sections, the FILE SECTION, the WORKING-STORAGE SECTION, the LOCAL-STORAGE SECTION, the LINKAGE SECTION, the REPORT SECTION and the SCREEN SECTION. If there are no entries for a section, the section name can be omitted. In the tutorial example, the only items that need to be declared are WORKING-STORAGE SECTION items, so that is the only section declared.
Within the WORKING-STORAGE section we have a number of declarations.
Each line specifies a level number, an identifier and a usage. For example, the statement
01 ix BINARY-C-LONG VALUE 0.
specifies a top-level field called ix, that is held as a 32-bit binary value and has an initial value of zero.
The power of COBOL starts becoming apparent when we look at the statement
01 display-number PIC Z(19)9.
This specifies a top-level field called display-number that has a picture clause which consists of 20 digits. If any of the leading digits are zero, then display them as spaces. Every time a value is moved into this field, this editing is honored.
The PROCEDURE DIVISION consists not of sections, but of labels and procedural statements (remember COBOL is not an object-oriented language but does support structured statements so that the GO TO statement only needs to be used on rare occasions.
The beauty of this language is that it is in English. The tutorial does not for example need to explain to you that the statement
ADD first-number TO second-number GIVING temp-number
add's the first-number data field to the second-number data field to give you a result placed into temp-number!
IDENTIFICATION DIVISION. PROGRAM-ID. "Fibonacci". ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 ix BINARY-C-LONG VALUE 0. 01 first-number BINARY-C-LONG VALUE 0. 01 second-number BINARY-C-LONG VALUE 1. 01 temp-number BINARY-C-LONG VALUE 1. 01 display-number PIC Z(19)9. PROCEDURE DIVISION. * This is the start of the program START-PROGRAM. MOVE first-number TO display-number. DISPLAY display-number. MOVE second-number TO display-number. DISPLAY display-number. PERFORM VARYING ix FROM 1 BY 1 UNTIL ix = 90 ADD first-number TO second-number GIVING temp-number MOVE second-number TO first-number MOVE temp-number TO second-number MOVE temp-number TO display-number DISPLAY display-number END-PERFORM. STOP RUN.
This post has been edited by Martyn.Rae: 02 May 2011 - 06:00 AM