In 1950’s there was a growing need for a high level programming language suitable for business data processing. To meet this demand the United States Department of Defense convened a conference on the 28th and 29th May 1958. Which was attended by users from civil and governmental organizations, computer manufacturers and other interested groups, out of this conference three groups were formed for the actual design of the language: one for short-term work, another for middle-term work and the third for long-term work.
In Sep. 1959, the short-term committee submitted a report to the directorate. In the same year the Directorate held a meeting and it was in this meeting that the new language was named COBOL (Common Business Oriented Language). Then COBOL was published in 1960.
On May 5 1961, COBOL-61 was published, the next version was published in 1965. In 1968, a standard version of the language was approved by American National Standard Institute(ANSI). This version is known as ANSI-68 COBOL (or) COBOL 68. The next version is COBOL-74. In 1985. a revised standard version was introduced. It is known as COBOL-85. It is the latest version of COBOL.
A program in COBOL is written as a sequence of lines. To facilitate writing of COBOL programs, printed forms known as COBOL CODING SHEET.
13467811 12727380
Page LineArea AArea BIdentification
NoNo
The 80 positions of a line are divided into areas. Every position in a line is also known as Column.
The COBOL Coding sheet has 80 columns.
The first six columns are called “Sequence number”, column. The first three columns used for page numbers. The columns 4 – 6 are used for line numbers.
Column 7 is called the continuation column. If we put “*” or “/” in column7 that line will not be executed by the computer. The symbol “/ “may be put in column 7 to cause program printing from a fresh page.
If we put “- “ in this column, it means if the statement is exceed more than 72 columns, the continuation should be expanded with the hyphen in column 7.
Column 8 to 11 is known as “Area A“(or) “Margin A”. Here we write all headers for example, division headers, section headers and paragraph headers, Level numbers also printed in this column.
Column 12 to 72 is known as “Area B” or “Margin B”. Here we write all COBOL statements. Area entries can be continued into Area B, if required.
Column 73 to 80 may be used for identification purpose. These columns are non-executed in all times. The use of this field is optional.
-------| Area A 000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. COMMENT. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 PROCEDURE DIVISION. 000600 000700* This is a comment. 000800* This paragraph displays information about the program. 000900 PROGRAM-BEGIN. ----------| Area B 003700DISPLAY "This program contains four DIVISIONS,". 003800DISPLAY "three PARAGRAPHS". 001000DISPLAY "and four SENTENCES". -------| Area A 001100 PROGRAM-DONE.
----------| Area B
001200STOP RUN.
ANALYSIS:As a historical note, the very first COBOL programs were written using punch cards. Each card carried one line of code that had been carefully entered using a keypunch machine (a kind of typewriter that punches holes in cards). The stack of punched cards was carried to the computer and fed into it using a card reader. An "out of sequence" warning was used to let you know that you probably had dropped the punch card deck and hadn't put them back together in the correct sequence. Compiler error messages also referred to line numbers, and locating an error was difficult without line numbers on the cards. PC COBOL compilers rarely give warnings about sequence.
Lines 000700 and 000800 contain an asterisk in column 7, the indicator area. Everything beyond the asterisk is ignored and can be used as a comment, as in the example.
DIVISIONs and paragraphs start in Area A but can extend into Area B.
Sentences begin and end in Area B. In Listing 1.5, sentences appear at lines 003700, 003800, 001000, and 001200.
What Is a Shell Program?
Because COBOL has a certain minimum amount of code that is required for all programs, it is a good practice to maintain a COBOL program that contains the minimum requirements.
New Term:A COBOL program that contains the minimum requirements usually is called a shell program, a skeleton program, or a boilerplate program.
If you copy this shell program to a new file before you start editing the new program, you can save yourself extra typing. Listing 1.6, cobshl01.cbl, is your first version of the COBOL shell program. As you progress through the days, you'll gradually add more and more pieces to the shell; eventually, you'll have a complete shell to use in all projects.
TYPE: Listing 1.6. Your first version of a COBOL shell.
Type Listing 1.6 and compile it to ensure that everything in it is correct. You will use this shell, and versions of it, many times before you are through with these lessons.
As you will see in the next few days, PC-based COBOL compilers are much less stringent about following the rules described in today's lesson. However, COBOL is intended to be a portable language that allows code to be moved to other computers. Another computer probably will be using another COBOL compiler that might require compliance to all the strict rules of COBOL. When you start breaking the rules, you limit your code to a compiler that can handle the loose syntax, and this might make it extremely difficult to move your code to another machine or compiler.
Summary
Today's lesson introduced you to some computer and programming basics, including the following:
A computer processes numbers and is made up of a central processing unit, input devices, output devices, main memory, and secondary storage.
A computer can't do anything without a program.
A program is a series of instructions that the central processing unit executes to process data, usually using some input data and providing some sort of output.
A programming language is a method of writing a source code file in an English-like language that a human being can understand.
A compiler translates the source code file into instructions that the central processing unit can understand and execute.
A COBOL program contains four DIVISIONs: the IDENTIFICATIONDIVISION, the ENVIRONMENT DIVISION, the DATA DIVISION, and the PROCEDURE DIVISION.
A DIVISION can be broken down into paragraphs. Some divisions have required paragraphs, such as the PROGRAM-ID paragraph in the IDENTIFICATION DIVISION.
The names of the paragraphs in the PROCEDUREDIVISION are assigned by the programmer.
The work of a COBOL program is done in sentences that contain the commands of a program and appear within a paragraph.
A COBOL program is written in 80-column format. The columns are divided into the following areas:
Columns 1 through 6 are the sequence area and can be used by the programmer for line numbering. Line numbering is optional.
Column 7 is the indicator area. An asterisk (*) in column 7 causes everything to the right of the asterisk to be treated as a comment.
Columns 8 through 11 are Area A, and columns 12 through 72 are Area B. DIVISION names, SECTION names, and paragraph names must begin in Area A, but they can extend into Area B. Sentences begin and end in Area B.
Columns 73 through 80 are undefined and are not processed by the COBOL compiler, but some COBOL editors use these columns to tag lines with modification codes.
Today, you also learned how to type, compile, and run several simple COBOL programs.
Q&A
Q Why is so much of COBOL in uppercase?
A COBOL was developed in the days when computer terminals and keypunch machines used only uppercase letters. The entire language was defined in terms of this all-uppercase state of affairs. You can display uppercase and lowercase messages, such as Hello world, because terminals now have uppercase and lowercase capability. However, the actual elements of COBOL--the DIVISIONs, the paragraph names such as PROGRAM-BEGIN, and the verbs such as DISPLAY--originally were designed in uppercase only.
Q What does a blank line in a program do?
A Nothing. The compiler skips blank lines. You can put blank lines anywhere you want in the program to improve readability.
Q Does COBOL use line numbers?
A Yes, but they are optional. Most compilers ignore them or can be set to ignore them. Some compilers on larger computers will process the line numbers and provide a warning if the line numbers are out of sequence, but this is not an error.
Q Can I put anything in a comment?
A Yes. As long as the asterisk appears at column 7, everything after the asterisk is ignored and has no effect on the compiler. You can write English sentences or gobbledygook, although the usual practice is to provide some information that describes what the program is doing, or why it is doing it.
> You can do this on as many lines as necessary to complete the comment.
Q Will my comments appear in the program when it runs?
A No. Comments appear only in the source code file, and they will not be included in the compiled program. Comments are intended to document the source code. The computer can read only the compiled code, and the comments wouldn't mean anything to the computer, so they are not included in the resulting program.
Q Why do paragraph names have hyphens in them?
A Paragraph names in COBOL are limited to 30 characters and may only contain uppercase letters (A through Z), digits (0 through 9), and the hyphen (-). The hyphen is used to improve the readability of paragraphs and the names of data (which is covered in Day 2). Some modern compilers allow paragraph names, division names, and other elements of COBOL to be typed in lowercase.
Note: Some compilers might not give an error on compiling BAD02, but might only provide a warning.
7. What is wrong with the following, bad03.cbl?
000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. BAD03. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 PROCEDURE DIVISION. 000600 This program displays a message. 000700 PROGRAM-BEGIN. 000800 DISPLAY "I'm really bad!". 000900 PROGRAM-DONE. 001000 STOP RUN.
Exercises
1. Modify the hello.cbl program to display I am a COBOL programmer.
Hint: Copy the hello.cbl program to iam.cbl:
copy hello.cbl iam.cbl
Then use your editor or pcobol to change the PROGRAM-ID and DISPLAY statements.
2. Use the computer to compile each of the bad examples (bad01.cbl, bad02.cbl, and bad03.cbl) to get a feel for the types of error messages that your compiler produces.
COBOL-85 compilers are much more relaxed than earlier standards. The ENVIRONMENT, DATA, and PROCEDURE DIVISIONs are optional under ANSI 85, although what a program would do without a PROCEDURE DIVISION is a bit of a mystery. The results of compiling these programs are interesting.
Both Micro Focus Personal COBOL and ACUCOBOL found nothing wrong with bad01.cbl. Apparently, if a program contains no data, it doesn't need to have a DATA DIVISION.
Micro Focus Personal COBOL handled bad02.cbl without a hiccup.
ACUCOBOL produced a warning that a sentence was starting in Area A.
Both Micro Focus Personal COBOL and ACUCOBOL generated errors on bad03.cbl and would not compile it.
3. Modify bad01.cbl so that it compiles without errors or warnings.
4. Modify bad02.cbl so that it compiles without errors or warnings.
5. Modify bad03.cbl so that it compiles without errors or warnings.
A sequence of continuous characters from the character set can form a word. The words can be classified into two types.
i) Reserved words
ii) User-defined words.
(i) Reserved words:-
Reserved words are words that are defined syntactically and semantically by the COBOL language.
Ex:-
ACCEPT, ADD, ALL, CALL, DATA, DELETE
(ii) User defined words:-
User defined words are words supplied by the programmers (language user) in order to satisfy the format of statement in the language.
Rules:-
A word must conform to the following rules:
i) The total number of characters must not be greater than 30.
ii) One of the characters must be a letter. Some compilers put the additional restrictions that the first character must be a letter.
iii) A word cannot begin or end with a hyphen..
iv) A word must not contain a blank and any special character except a hyphen.
Ex:-
ValidInvalid
GROSS-PAY-GROSS
BOVERTIME HOURS
Z31-41-2-3
Structure of COBOL Program:-
A COBOL program must have four divisions. They are,
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION
Out of these divisions, the PROCEDURE DIVISION is one where the algorithm is described in terms of some elements of the COBOL language called statements.
vEvery statement begins with a verb which indicates the kind of operation that is to be performed during the execution of the statement.
vUsually a statement is terminated by a period (.)
vTwo or more statements can also be separated by spaces or comma followed by a space. A group of such statements with the last one having a terminating period is called a sentence.
vIn COBOL, names for data fields are called data names.
vAll the data names which are used in the PROCEDURE DIVISION statements must be defined in the DATA DIVISION.
vThe definition of data name should include the size of the data item, its type, etc.
vData names that hold the results of intermediate computations should appear in the WORKING-STORAGE SECTION of the DATA DIVISION.
vThe ENVIRONMENT DIVISION consists of two sections – the CONFIGURATION SECTION and INPUT-OUTPUT SECTION.
vThe IDENTIFICATION DIVISION consists of a number of paragraphs showing the name of the program, name of its author, date on which the program is compiled and similar information.
vAn entry or a statement of a COBOL program can be written in one or more coding lines.
Data names and Identifiers
A data name gives reference to the storage space in the memory where the actual value is stored. This value takes part in the operation when that particular data name is used in the PROOCEDURE DIVISION.
Data names are one form of identifiers. A data name must be a user-defined word and cannot be a reserved word.
Literals
Literals are constants. It refers constant values used in programs. These literals can be numeric, non-numeric or figurative constant.
(i)Numeric Literals:-
Numeric literals can be formed with the help of digits only. It is used in arithmetic operations and calculations.
Rules:-
i)It should be made up of only digits (0 – 9), positive or negative sign (+ or -) and a decimal point (.).
ii)The positive or negative sign should be before the number and there should be no blank spaces between the sign and the numbers.
iii)The use of decimal point should be followed by another at least one digit.
iv)The biggest number that can be represented in COBOL can have a maximum of 18 digits.
Ex:-
ValidInvalid
+20.120+
-31.0“150”
(ii) Non-numeric literals:-
String of characters grouped together is known as non-numeric literals. It is used to output messages or headings. Even if the non-numeric literal is made up of only digits, these digits cannot be considered for arithmetic calculations.
Rules:-
i)The non-numeric literals should be enclosed with in quotation marks (“ “).
ii)The length o0f non-numeric literals should not exceed 120 characters.
(iii)Figurative constants:-
It represents values that may be frequently used by most programs.
List of figurative constants:
i)ZERO, ZEROS AND ZEROES can be used to represent a 0.
ii)SPACE OR SPACES can be used to represent blanks.
iii)QUOTE OR QUOTES can be used to represent “ “.
iv)HIGH-VALUES, LOW-VALUES and all literals are the other figurative constants used in COBOL.
The IDENTIFICATION DIVISIION is the first division of every COBOL source program. There may be several paragraphs in this division of which the paragraph PROGRAM-ID is the essential in most of the machines. The other paragraphs are optional and may be used mainly for documentation purposes.
IDENTIFICATION DIVISION.
PROGRAM-ID. Entry.
[AUTHOR.] Entry.
[INSTALLATION.] Entry.
[DATE-WRITTEN.] Entry.
[DATE-COMPILED.] Entry.
[SECURITY.] Entry.
The following shows the structure of this division
The division heading and paragraph names should be coded as area A entries. Each of the paragraph names must end with a period followed by at least one blank space. The entries following the paragraph heading must be terminated by a period. The entry in the PROGRAM-ID paragraph contains the program name to be used to identify the object program. The program name can consist of 1 to 30 characters and must contain at least one letter.
The entries in the other paragraphs are normally treated as comments.
ENVIRONMENT DIVISION:
The ENVIRONMENT DIVISION is the division that must follow the IDENTIFICATION DIVISION in a COBOL source program. Among all the four division this one is the most machine-dependent division. The computer and all peripheral devices required by the program are described in this division.
This division contains two sections.
vCONFIGURATION SECTION
vINPUT-OUTPUT SECTION
The out-line of the sections and paragraphs of this division is shown below.
This section contains an overall specification of the computer used for the purpose of compilation and execution of the program. There are in all three paragraphs in this section.
SOURCE-COMPUTER:
This paragraph specifies the name of the computer used to compile the COBOLprogram.
The following is the form of this paragraph.
SOURCE-COMPUTER. Computer-name.
Ex:-
SOURCE-COMPUTER. ICL-1901.
OBJECT-COMPUTER:
The OBJECT-COMPUTER paragraph describes the computer on which the program is to be executed.
The following shows the syntax for this paragraph.
OBJECT-COMPUTER. Computer-name
[, MEMORY SIZE integer-1CHARACTERS]
WORDS
[, PROGRAM COLLALTING SEQUENCE IS alphabet-name]
[, SEGMENT-LIMT IS integer-2].
The computer name specifies a particular computer on which the object program is to be executed.
The MEMORY SIZE is used to indicate the amount of storage available to the object program.
The PROGRAM COLLATING SEQUENCE clause specifies the collating sequence that is to be used to compare non-numeric data items.
The SEGMENT-LIMIT clause is used in most of the compilers to indicate that the sections having segment number less than the number specified in integer-2.
Ex:-
OBJECT-COMPUTER. ICL-1900
MEMORY SIZE 8000 WORDS.
SPECIAL-NAMES:
This paragraph is used to relate some hardware names to user-specified mnemonic names. This paragraph is optional in all compilers. The following is the format of this paragraph.
SPECIAL-NAMES. [, CURRENCY SIGN IS literal-1]
[, DECIMAL-POINTISCOMMA]
[, CHANNEL. Integer IS mnemonic-names]…
[, alphabet ALPHABET-NAME ISSTANDARD-1
NATIVE] …
Implementor-name
[, implementor-name IS mnemonic-name].
The CHANNEL clause is used to control the line spacing of line printers.
The ALPHABET clause specifies a user-defined alphabet name that can be used to indicate a collating sequence in the PROGRAM COLLATING SEQUENCE clause.
Ex:-
SPECIAL-NAMES. CHANNEL 1 IS PAGE-TOP.
INPUT-OUTPUT SECTION:
The section contains information regarding files to be used in the program. There are two paragraphs in this section – FILE-CONTROL and I-O-CONTROL.
FILE-CONTROL:
The FILE-CONTROL paragraph names each file and identifies the file medium through file control entries.
The simplified format of a file control entry is given below.
SELECT [OPTIONAL] file-name ASSIGN TO hardware-name.
In general, a COBOL source program uses some files. For each of these files, there must be a FILE-CONTROL entry. This entry names the file and assigns a peripheral device which holds that particular file. The file names that appear in the SELECT clause must be unique and all these files must be described in DATA DIVISION. The file name should be formed according to the rules of data names.
The word OPTIONAL may be used only for input files.
DATA DIVISION is that part of a COBOL program where every data item processed by the program is described. It is important to note that unless a data item is described in the DATA DIVISION, it cannot be used in the procedure division. The DATA DIVISION is divided into a number of sections and depending on the use of a data item, it should be defined in the appropriate section.
The format of the DATA DIVISION is as follows:
DATA DIVISION.
[ FILE SECTION.
File section entries.
……….
………. ]
[WORKING-STORAGE SECTION.
Working-storage entries.
………
……… ]
(a)FILE SECTION:
The FILE SECTION includes the descriptions of all data items that should be read from or written onto some external file.
(b) WORKING-STORAGE SECTION
LEVEL STRUCTURE:-
The data to be processed are internally stored in a specific area in the memory of a computer.
In COBOL a distinction is made between elementary and group data items. A few elementary data may be combined to form a group. For example, DAY, MONTH and YEAR may be three elementary data items. These may be combined to form a group data named DATE.
The organization may be shown pictorially as follows:
DATE
DAY
MONTH
YEAR
It may be noted that the memory space referred to by DATE is the combined memory space for DAY, MONTH and YEAR.
The programmer can now refer to the individual elementary items DAY, MONTH, YEAR or to the group item DATE.
To describe the hierarchical structure introduced above, the concept of level number is employed in COBOL.