Sunday, September 21, 2008

COBOL Sample Program - Implements a Primative calculator

An example program that implements a primative calculator. The calculator only does additions and multiplications.

$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. Iteration-If.
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.
01 Operator PIC X VALUE SPACE.

PROCEDURE DIVISION.
Calculator.
PERFORM 3 TIMES
DISPLAY "Enter First Number : " WITH NO ADVANCING
ACCEPT Num1
DISPLAY "Enter Second Number : " WITH NO ADVANCING
ACCEPT Num2
DISPLAY "Enter operator (+ or *) : " WITH NO ADVANCING
ACCEPT Operator
IF Operator = "+" THEN
ADD Num1, Num2 GIVING Result
END-IF
IF Operator = "*" THEN
MULTIPLY Num1 BY Num2 GIVING Result
END-IF
DISPLAY "Result is = ", Result
END-PERFORM.
STOP RUN.

Saturday, August 30, 2008

Sample Program - PERFORM

An example program that demonstrates how the first format of the PERFORM may be used to change the flow of control through a program.

$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. PerformFormat1.
AUTHOR. Michael Coughlan.
* Illustrates how the first format of the PERFORM may
* be used to change the flow of control through a program.
* Use the output of this program to get an understanding of how
* this format of the PERFORM works.

PROCEDURE DIVISION.
TopLevel.
DISPLAY "In TopLevel. Starting to run program"
PERFORM OneLevelDown
DISPLAY "Back in TopLevel.".
STOP RUN.


TwoLevelsDown.
DISPLAY ">>>>>>>> Now in TwoLevelsDown."
PERFORM ThreeLevelsDown.
DISPLAY ">>>>>>>> Back in TwoLevelsDown.".


OneLevelDown.
DISPLAY ">>>> Now in OneLevelDown"
PERFORM TwoLevelsDown
DISPLAY ">>>> Back in OneLevelDown".


ThreeLevelsDown.
DISPLAY ">>>>>>>>>>>> Now in ThreeLevelsDown".

Friday, July 18, 2008

COMPUTE Commands


The COMPUTE assigns the result of an arithmetic expression to a data-item. The arithmetic expression is evaluated according to the normal arithmetic rules. That is, the expression is normally evaluated from left to right but bracketing and the precedence rules shown below can change the order of evaluation.

Precedence Symbol Meaning
1. ** Power
2. * Multiply
/ Divide
3. + Add
- Subtract

Note that unlike some other programming languages COBOL provides the ** expression symbol to represent raising to a power.

Saturday, July 12, 2008

ACCEPT Command

The ACCEPT verb is used to get data from the keyboard, a peripheral device, or certain system variables.

ACCEPT notes
When the first format is used, the ACCEPT inserts the data typed at the keyboard (or coming from the peripheral device), into the receiving data-item.

When the second format is used, the ACCEPT inserts the data obtained from one of the system variables, into the receiving data-item.
Using the ACCEPT to get the system date and time

The second format of the ACCEPT allows the programmer to access the system date and time (i.e.the date and time held in the computer's internal clock). The system variables provided are -

* Date
* Day of the year
* Day of the week
* Time

The declarations and comments below show the format required for data-items receiving each of the system variables.

01 CurrentDate PIC 9(6).
* CurrentDate is the date in YYMMDD format

01 DayOfYear PIC 9(5).
* DayOfYear is current day in YYDDD format

01 Day0fWeek PIC 9.
* DAY-OF-WEEK is a single digit where 1=Monday

01 CurrentTime PIC 9(8).
* CurrentTime is the time in HHMMSSss format where s = S/100



New formats for the ACCEPT

The problem with ACCEPT ..FROM DATE and ACCEPT..FROM DAY is that since they hold only the year in only two digits, they are subject to the millennium bug. To resolve this problem, these two formats of now take additional (optional) formatting instructions to allow the programmer to specify that the date is to be supplied with a 4 digit year.

The syntax for these new formatting instructions is:

ACCEPT DATE [YYYYMMDD]
ACCEPT DAY [YYYYDDD]

When the new formatting instructions are used, the receiving fields must be defined as;

01 Y2KDate PIC 9(8).
* Y2KDate is the date in YYYYMMDD format

01 Y2KDayOfYear PIC 9(7).
* Y2KDayOfYear is current day in YYYYDDD format

ACCEPT and DISPLAY example program


This example program uses the ACCEPT and DISPLAY to get a student record from the user and display some of its fields. It also demonstrates how the ACCEPT can be used to get the system date and time.

$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. AcceptAndDisplay.
AUTHOR. Michael Coughlan.
* Uses the ACCEPT and DISPLAY verbs to accept a student record from the user and display some of the fields. Also shows how
* the ACCEPT may be used to get the system date and time. The YYYYMMDD in "ACCEPT CurrentDate FROM DATE YYYYMMDD."
* is a format command that ensures that the date contains a 4 digit year. If not used, the year supplied by the system
* will only contain two digits which may cause a problem in the year 2000.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 CourseCode PIC X(4).
02 Gender PIC X.

* YYMMDD
01 CurrentDate.
02 CurrentYear PIC 9(4).
02 CurrentMonth PIC 99.
02 CurrentDay PIC 99.

* YYDDD
01 DayOfYear.
02 FILLER PIC 9(4).
02 YearDay PIC 9(3).


* HHMMSSss s = S/100
01 CurrentTime.
02 CurrentHour PIC 99.
02 CurrentMinute PIC 99.
02 FILLER PIC 9(4).


PROCEDURE DIVISION.
Begin.
DISPLAY "Enter student details using template below".
DISPLAY "Enter - ID,Surname,Initials,CourseCode,Gender"
DISPLAY "SSSSSSSNNNNNNNNIICCCCG".
ACCEPT StudentDetails.
ACCEPT CurrentDate FROM DATE YYYYMMDD.
ACCEPT DayOfYear FROM DAY YYYYDDD.
ACCEPT CurrentTime FROM TIME.
DISPLAY "Name is ", Initials SPACE Surname.
DISPLAY "Date is " CurrentDay SPACE CurrentMonth
SPACE CurrentYear.
DISPLAY "Today is day " YearDay " of the year".
DISPLAY "The time is " CurrentHour ":" CurrentMinute.
STOP RUN.


Results of running ACCEPT.CBL

Enter student details using template below
Enter - ID,Surname,Initials,CourseCode,Gender
SSSSSSSNNNNNNNNIICCCCG
9923453Power NSLM51F
Name is NS Power
Date is 01 03 1999
Today is day 060 of the year
The time is 14:41

Sunday, July 6, 2008

MOVE Command

MOVE Source$#il TO Destination$#i ...

As we can see from the syntax metalanguage above, the MOVE copies data from the source identifier or literal to one or more destination identifiers.

Although this sounds simple, the actual operation of the MOVE is somewhat more complicated and is governed by a number of rules.

MOVE rules
In most other programming languages, data is assigned from the source item on the right to the destination item on the left (e.g. Qty = 10;) but in COBOL the MOVE assigns data from left to right. The source item is on the left of the word TO and the receiving item(s) is on the right.

The source and destination identifiers can be group or elementary data-items.

When data is moved into an item, the contents of the item are completely replaced.

If the number of characters in the source data-item is less than the number in the destination item, the rest of the destination item is filled with zeros or spaces.

If the source data-item is larger than the destination item, the characters that cannot fit into the destination item will be lost. This is known as truncation.

When the destination item is alphanumeric or alphabetic (PIC X or A), data is copied into the destination area from left to right with space filling or truncation on the right.

When the destination item is numeric, or edited numeric, data is aligned along the decimal point with zero filling or truncation as necessary.

When the decimal point is not explicitly specified in either the source or destination items, the item is treated as if it had an assumed decimal point immediately after its rightmost character.

MOVE examples

MOVE num1 TO num2.
MOVE 1 TO EOFSW.
MOVE 0 TO a b c.

Thursday, June 26, 2008

ACCEPT and DISPLAY example program

This example program uses the ACCEPT and DISPLAY to get a student record from the user and display some of its fields. It also demonstrates how the ACCEPT can be used to get the system date and time.


$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. AcceptAndDisplay.
AUTHOR. Michael Coughlan.
* Uses the ACCEPT and DISPLAY verbs to accept a student record from the user and display some of the fields. Also shows how
* the ACCEPT may be used to get the system date and time. The YYYYMMDD in "ACCEPT CurrentDate FROM DATE YYYYMMDD."
* is a format command that ensures that the date contains a 4 digit year. If not used, the year supplied by the system
* will only contain two digits which may cause a problem in the year 2000.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 CourseCode PIC X(4).
02 Gender PIC X.

* YYMMDD
01 CurrentDate.
02 CurrentYear PIC 9(4).
02 CurrentMonth PIC 99.
02 CurrentDay PIC 99.

* YYDDD
01 DayOfYear.
02 FILLER PIC 9(4).
02 YearDay PIC 9(3).


* HHMMSSss s = S/100
01 CurrentTime.
02 CurrentHour PIC 99.
02 CurrentMinute PIC 99.
02 FILLER PIC 9(4).


PROCEDURE DIVISION.
Begin.
DISPLAY "Enter student details using template below".
DISPLAY "Enter - ID,Surname,Initials,CourseCode,Gender"
DISPLAY "SSSSSSSNNNNNNNNIICCCCG".
ACCEPT StudentDetails.
ACCEPT CurrentDate FROM DATE YYYYMMDD.
ACCEPT DayOfYear FROM DAY YYYYDDD.
ACCEPT CurrentTime FROM TIME.
DISPLAY "Name is ", Initials SPACE Surname.
DISPLAY "Date is " CurrentDay SPACE CurrentMonth
SPACE CurrentYear.
DISPLAY "Today is day " YearDay " of the year".
DISPLAY "The time is " CurrentHour ":" CurrentMinute.
STOP RUN.

ACCEPT Command

The ACCEPT verb is used to get data from the keyboard, a peripheral device, or certain system variables.

ACCEPT notes
When the first format is used, the ACCEPT inserts the data typed at the keyboard (or coming from the peripheral device), into the receiving data-item.

When the second format is used, the ACCEPT inserts the data obtained from one of the system variables, into the receiving data-item.

Using the ACCEPT to get the system date and time

The second format of the ACCEPT allows the programmer to access the system date and time (i.e.the date and time held in the computer's internal clock). The system variables provided are -

* Date
* Day of the year
* Day of the week
* Time

The declarations and comments below show the format required for data-items receiving each of the system variables.

01 CurrentDate PIC 9(6).
* CurrentDate is the date in YYMMDD format

01 DayOfYear PIC 9(5).
* DayOfYear is current day in YYDDD format

01 Day0fWeek PIC 9.
* DAY-OF-WEEK is a single digit where 1=Monday

01 CurrentTime PIC 9(8).
* CurrentTime is the time in HHMMSSss format where s = S/100



New formats for the ACCEPT

The problem with ACCEPT ..FROM DATE and ACCEPT..FROM DAY is that since they hold only the year in only two digits, they are subject to the millennium bug. To resolve this problem, these two formats of now take additional (optional) formatting instructions to allow the programmer to specify that the date is to be supplied with a 4 digit year.

The syntax for these new formatting instructions is:

ACCEPT DATE [YYYYMMDD]
ACCEPT DAY [YYYYDDD]

When the new formatting instructions are used, the receiving fields must be defined as;

01 Y2KDate PIC 9(8).
* Y2KDate is the date in YYYYMMDD format

01 Y2KDayOfYear PIC 9(7).
* Y2KDayOfYear is current day in YYYYDDD format

Monday, June 16, 2008

DISPLAY Command

DISPLAY OutputItem1$#il[OutputItem2#il]...
[UPON Mnemonic-Name][WITH NO ADVANCING]

The DISPLAY verb is used to send output to the computer screen or to a peripheral device.

As you can see from the ellipses (...) in the metalanguage above a single DISPLAY can be used to display several data-items or literals or any combination of these.

DISPLAY notes

After the items in the display list have been sent to the screen, the DISPLAY automatically moves the screen cursor to the next line unless a WITH NO ADVANCING clause is present.

Mnemonic-Names are used to make programs more readable. A Mnemonic-Name is a name devised by the programmer to represent some peripheral device (such as a serial port) or control code. The name is connected to the actual device or code by entries in the ENVIRONMENT DIVISION.

When a Mnemonic-Name is used with the DISPLAY it represents an output device (serial port, parallel port etc).

If a Mnemonic-Name is used output is sent to the device specified; otherwise, output is sent to the computer screen.

DISPLAY examples

DISPLAY "My name is " ProgrammerName.
DISPLAY "The vat rate is " VatRate.
DISPLAY PrinterSetupCodes UPON PrinterPort1.

Basic Commands

Some COBOL Commands

The PROCEDURE DIVISION contains the code used to manipulate the data described in the DATA DIVISION. This page examines some of the basic COBOL commands used in the PROCEDURE DIVISION.

DISPLAY.

ACCEPT.

MOVE.

COMPUTE.

The DATA DIVISION

As the name suggests, the DATA DIVISION provides descriptions of the data-items processed by the program.

The DATA DIVISION has two main sections: the FILE SECTION and the WORKING-STORAGE SECTION. Additional sections, such as the LINKAGE SECTION (used in subprograms) and the REPORT SECTION (used in Report Writer based programs) may also be required.

The FILE SECTION is used to describe most of the data that is sent to, or comes from, the computer's peripherals.

The WORKING-STORAGE SECTION is used to describe the general variables used in the program.


The DATA DIVISION has the following structure and syntax:

DATA DIVISION.
FILE SECTION.
File Section Entries.
WORKING-STORAGE SECTION.
WS entries.

Below is a sample program fragment -


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.

The ENVIRONMENT DIVISION

The ENVIRONMENT DIVISION is used to describe the environment in which the program will run.

The purpose of the ENVIRONMENT DIVISION is to isolate in one place all aspects of the program that are dependant upon a specific computer, device or encoding sequence.

The idea behind this is to make it easy to change the program when it has to run on a different computer or one with different peripheral devices.

In the ENVIRONMENT DIVISION, aliases are assigned to external devices, files or command sequences. Other environment details, such as the collating sequence, the currency symbol and the decimal point symbol may also be defined here.


ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-PC.
OBJECT-COMPUTER. IBM-PC.

INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ACC-INPUT-FILE
ASSIGN TO INPUT1
ORGANIZATION IS SEQUENTIAL.

SELECT ACC-OUTPUT-FILE
ASSIGN TO OUTPUT1
ORGANIZATION IS SEQUENTIAL.

The IDENTIFICATION DIVISION

The IDENTIFICATION DIVISION supplies information about the program to the programmer and the compiler.

Most entries in the IDENTIFICATION DIVISION are directed at the programmer. The compiler treats them as comments.

The PROGRAM-ID clause is an exception to this rule. Every COBOL program must have a PROGRAM-ID because the name specified after this clause is used by the linker when linking a number of subprograms into one run unit, and by the CALL statement when transferring control to a subprogram.

The IDENTIFICATION DIVISION has the following structure:

IDENTIFICATION DIVISION
PROGRAM-ID. NameOfProgram.
[AUTHOR. YourName.]
other entries here

The keywords - IDENTIFICATION DIVISION - represent the division header, and signal the commencement of the program text.

PROGRAM-ID is a paragraph name that must be specified immediately after the division header.

NameOfProgram is a name devised by the programmer, and must satisfy the rules for user-defined names.

Here's a typical program fragment:

IDENTIFICATION DIVISION.
PROGRAM-ID. SequenceProgram.
AUTHOR. Michael Coughlan.

Four Divisions

At the top of the COBOL hierarchy are the four divisions. These divide the program into distinct structural elements. Although some of the divisions may be omitted, the sequence in which they are specified is fixed, and must follow the order below.

IDENTIFICATION DIVISION
Contains program information

ENVIRONMENT DIVISION
Contains environment information

DATA DIVISION
Contains data descriptions

PROCEDURE DIVISION
Contain the program algorithms

COBOL History

In 1952, Grace Murray Hopper began a journey that would eventually lead to the language we know as COBOL.

She began by developing a series of programming languages that became more and more like natural language. The language used phrases to express the operations of business data processing. FLOWMATIC was the result of this evolutionary journey.

Through the 1950's, other computing leaders were also working through the challenge of creating a practical business language. IBM had produced a language named COMMERCIAL TRANSLATOR.

In 1959, an industry-wide team was assembled to formulate a common business programming language. The Conference on Data System Languages (CODASYL) led by Joe Wegstein of National Bureau of Standards (now National Institute of Standards and Technology) developed a new language, and created the first standardized business computer programming language.

COBOL (Common Business Oriented Language) was developed under the auspices of the U.S. Department of Defense in cooperation with computer manufactures, users and universities. The initial specifications for COBOL were presented in a report of the executive committee of CODASYL committee in April of 1960. It was designed to be a business problem oriented, machine independent and capable of continuous change and development.

Since 1960,COBOL has undergone considerable updates and improvements. It has emerged as the leading data processing language in the business world. The standard language specification has three levels low,middle and high so that standard COBOL can be implemented on computers of varying sizes.

Despite the attempts at standardization, variations in COBOL implementations continue to exist. Most deviations or "extensions" are intended to take advantage of hardware or environmental features which were not defined in the standard definition.


1968 -- American National Standards Institute (ANSI) developed a standard form of the language known as American National Standard (ANSI) COBOL. This was an attempt to overcome the incompatibilities of the different versions of COBOL.

1974 -- ANSI published a revised version of (ANSI) COBOL

1985 -- ANSI published another revised version

Today -- ISO and ANSI committees have completed the features of the latest revision of COBOL 2002.