Saturday 13 August 2011

COBOL Basics

COBOL programs are entered in predefined formats i.e., formatted acceptable by the COBOL compilers. For this reason, COBOL programs are written on COBOL coding sheets, which use a standard format. There are 80 characters positions on each line of the coding sheet and these positions are grouped into the following 5 fields.

RULES FOR CODING A COBOL PROGRAM OR MARGIN RULES


1-6 Sequence numbers
7 Indicator
8-11 Area A / Margin A
12-72 Area B / Margin B
73-80 Identification
73-81 





Columns 1 – 6
Line Numbers: Reserved for line numbers automatically assigned by the Compiler. These numbers are not used in the actual manipulations within the program.

Column 7
Indicator Area:- Documentation (‘*’ or ‘/’). A ’ * ’ is put in this column so that the Compiler can ignore the sentence. Continuation ( - hyphen) in this column specifies  that the commands continue onto the next line.

Column 8 – 11
Are reserved for programming statements.
Area A:
The following must begin in area A:
 Division and section headers
 Paragraph names
Declaratives and End Declaratives
End Program header
Level indicator (FD or SD)or level-number (01 or 77)

Column 12 – 72
Area B:
The following must begin in area B:
 Entries, sentences, statements, clauses

Column 73 onwards
Is ignored by the Compiler
 

COBOL LANGUAGE ELEMENTS :--

In  COBOL Language There are mainly 7 Elements are there , that are
  1. Cobol Characters
  2. Words
  3. Statements
  4. Sentences
  5. Paragraphs
  6. Sections
  7. Divisions
1. Cobol Characters :--

  • Alphabetic Characters   : A - Z, ‘ - ‘ i.e. hyphen preceded or followed by either a  letter or  a digit.
  • Numeric Characters      :  0 - 9
  • Punctuation Characters : , . ; ‘ (  )
  • Special Characters         : + - / * = > < . $
 The hyphen may NOT appear as the first or last character.Lowercase is equivalent to uppercase (except in non-numeric literals).Non-numeric literals and comments may contain any of the characters from the character set of the computer. 
NOTE: The program-name in PROGRAM-ID paragraph must begin with an alphabetic and hyphen(s) will be changed to zero(s).

2. Words :- 
 
COBOL is based on syntax rules and words derived from the English Language. Any violation of the rules would result in compilation errors. The 2 kinds of words used in COBOL are :
  • Reserved Words
  • User-Defined Words
Reserved words : A reserved word is a character-string with a predefined meaning in a COBOL source program. There are several types of reserved words in COBOL.
User-Defined Words: Unlike the reserved word the user-defined word is constructed and used by the application programmer. The following are the rules to be followed while framing user-defined words.


 Word Naming Rules :--

  • Length may be up to 30 characters
  • Only alphabets, digits and hyphen (-) are allowed
  • Embedded blanks are not allowed
  • At lease one character must be alphabetic
  • Cannot be a Cobol reserved word
  • May not begin or end with a hyphen
Sections and Paragraphs :-

         Each division of a COBOL program is broken down into sections beginning with special line called section header and each section in turn broken down to paragraph.

Section header consists of the name of the section followed by the word “SECTION” and a period. Section name in environment division and data division are a fixed part of COBOL program and cannot be changed. The Identification division has no paragraph.

Paragraph in COBOL may be part of a section or can stand alone with their division. In all cases paragraphs are identified by a paragraph header, which consists of name of the paragraph followed by a period.

Paragraph in the procedure division are made up of sentences. Although COBOL allows a period at the end of every statement.

 7.  Divisions :: -

         A COBOL program always consists of four main parts, called division. They are 
  • Identification Division
  • Environment Division
  • Data Division
  • Procedure Division   
  Identification Division :--
                  The IDENTIFICATION DIVISION is the first division of every COBOL source program. There may be several paragraphs in this division of which the paragraph PROGRAM-ID is essential in most of the machines. The other paragraphs are optional and may be used mainly for documentation purpose. The following shows the structure of this division.

IDENTIFICATION DIVISION
PROGRAM-ID. ENTRY
[AUTHOR. ENTRY]
[INSTALLATION. ENTRY]
DATE.WRITTEN.ENTRY]
[DATE-COMPILED. ENTRY.]
SECURITY. ENTRY.]

The division heading and paragraph names should be coded as area A entries. Each of the paragraph names must end with a period sign followed by at least one blank space. The PROGRAM-ID paragraph contains the program name to be used to identify the object program. The entries in the other paragraphs are normally treated as comments and the programmer is free to write anything for these entries. The author paragraph may include the name of the programmer. The date-compiled paragraph may contain the date of compilation.

Environment Division :-
The ENVIRONMENT DIVISION is the division that must follow the IDENTIFICATION Division in a COBOL source Program. Among all the four divisions 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- CONFIGURATION SECTION and INPUT-OUTPUT SECTION. Of these the CONFIGURATION SECTION appears first. The out line of the sections and paragraphs of this division is shown below.

ENVIRONMENT DIVISION
CONFIGURATION SECTION
SOURCE-COMPUTER. Source-Computer-entry.
OBJECT-COMPUTER. Object-Computer-entry
[SPECIAL NAMES. Special-names-entry]
[INPUT-OUTPUT SECTION.
FILE-CONTROL. {File-control-entry}
[I-O-CONTROL. Input-output-control-entry].]
CONFIGURATION SECTION
This section contains an overall] 1 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 COBOL program. The following is the form of this paragraph.

SOURCE-COMPUTER. computer name.

For example: If ICL 1901 is to be used for compiling the COBOL Source Program, this paragraph should be as follows:
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-1 CHARACTERS
WORDS
[PROGRAM COLLATING SEQUENCE IS alphabet-name]
[SEGMENT-LIMIT 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. This clause is also used in conjunction with the SORT Verb. The PROGRAM COLLATING SEQUENCE clause specifies the collating sequence that is to be used to compare nonnumeric data items. The alphabet name in this clause should be defined in the SPECIAL-NAMES paragraph to specify a collating sequence.

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-POINT IS COMMA]
[CHANNEL integer is mnemonic-names]…..
[ALPHABET alphabet-name IS STANDARD
NATIVE
Implementer -name
[ , implementer-name IS mnemonic-name ].

The CHANNEL clause is used to control the line spacing of the line printers.
The ALPHABET clause specifies a user-defined alphabet name that can be used to indicate a collating sequence in a PROGRAM COLLATING SEQUENCE clause. NATIVE – computer’s own collating sequence. STANDARD – ASCII collating sequence. The alphabet name is also used to define the external character set in which the data is recorded on a file.

EX: SPECIAL-NAMES. CHANNEL 1 IS PAGE-TOP.

INPUT-OUTPUT SECTION

This section contains information regarding files to be used in the program. There are two paragraphs in this section- FILE CONTROL and I-O-CONTROL. Of these, the first one is used in almost every program. In the following some of the entries of the FILE- CONTROL Paragraph will be discussed.

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.

For each of the file that we are using in program must have 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 SELECT clause must be unique and all these files must be described in DATA DIVISION.The word OPTIONAL may be used only for input files. The ASSIGN clause assigns a particular physical peripheral device name ( READER, PRINTER, TAPE and DISK) to a file.

EX:
FILE-CONTROL.
SELECT FILE1 ASSIGN TO READER.

Data Division :--

The 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. For the time being only two of the sections of the DATA DIVISION will be considered. These are as follows:

(a) FILE SECTION
The FILE SECTION includes the descriptions of all data items that should be read from or written on to some external file.

(b) WORKING-STORAGE SECTION
The data items which are developed internally as intermediate results as well as the constants are described in this section of the DATA DIVISION,.
The Format of the DATA DIVISION is as follows:
DATA DIVISION
[FILE SECTION
File Section entries.
………..
……….. ]
[WORKING –STORAGE SECTION
Working-Storage entries
……….. ]
5.2 LEVEL STRUCTURE
The data to be processed are internally stored in a specific area in the memory of a computer. The area corresponding to a Particular data item is referenced by the data name used in the description of the said item. Data names are user-created words. The rules for forming such names or words in COBAL have been discussed earlier. However, it is important to note that while the data name actually stands for a particular area in the memory, it is the content of the area that takes part in the operation when referred to by the said data name in the PROCEDURE DIVISION Statements.
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

Example 1
01 DATE
05 DAY
05 MONTH
05 YEAR

5.3. DATA DESCRIPTION ENTRIES
A data description entry describes a data item. It consists of a level number, data name (or FILLER) followed by a number of optional clauses terminated by a period. The purpose of an individual Clause is to specify certain characteristics of the data item being described.
5.3.1 PICTURE Clause
The picture clause describes the general characteristics of an elementary data item.
a.) Class : In COBOL a data item may be one of the 3 classes – numeric(0 – 9), alphabet(A-Z, space) or alphanumeric( digits, letters & special characters).
b.) Sign : A numeric data item can be signed or unsigned.
c.) Point location : It is specified for numeric data items. If it is not specified, the item is considered to be an integer which means that the decimal point is positioned immediately after the right most digits.
d.) Size : Specifies the number of characters or digits required to store the data item in a memory.

Syntax: PICTURE IS character-string
PIC

The character string can consist of 1 to 30 code characters.
Code characters meaning
9 Data item is numeral
X Data item is of any character from COBOL character set.
A Data item contains letter or space
V Position of the decimal point
P Assumed when point lies outside the data item
S Data item is signed

The allowable combinations are governed by the following rules:
(i) In the case of an alphabetic item the picture may contain only the symbol A.
(ii) In the case of a numeric item the picture may contain only the symbols 9, v, p and S. these are called operational characters. It must contains at least one 9. The symbol is V and S can appear only once and S, if t included, must be the leftmost character or on left (but not on the left of S) as may times as is required to indicate the position of the assumed decimal point.
(iii) In the Case of an alphanumeric item, the picture may Contain all XS or a combination of 9, A and X (Except all 9 or all A). In the latter case the item is considered as if the string consists of all XS.
Example:
PICTURE IS S999V99.

5.3.2 VALUE Clause
The Value Clause defines the initial value of a data item. Normally the initialization is done just before the first statement in the PROCEDURE DIVISION is executed. The syntax of the VALUE Clause in its most simple form is
VALUE IS literal

Examples
VALUE IS 3.5
VALUE IS “ MY DATA ”
VALUE ZERO.



5.4 FILE SECTION

The FILE SECTION must contain a file description entry followed by one or more record description entries for each of the files used in a program. The file description entry must begin with the level indicator FD followed to by the file name. This file name must be identical with the file name specified in the select clause of the ENVIRONMENT DIVISION.

FD file –name LABEL RECORDS ARE STANDARD
RECORD IS OMITTED

FD CARD-FILE
01 CARD-RECORDS
02 SALESMAN-NO PIC 9(5)
02 FILLER PIC X (5)
03 QUANTITY PIC 9 (5)
02 FILLER PIC X (5)
02 UNIT-PRICE PIC 9 (8) v99.
02 AMOUNT PIC 9(8) v 99
02 FILLER PI X (40)

5.5 WORKING-STORAGE Section

The data in the working storage can be a group item containing all its subdivisions as in the case of record description. There may also be elementary data items which do not belong to a group. Such data items should be defined at a special level number 77 and must begin in area A.

EX:
WORKING-STORAGE SECTION.
77 A PIC X(20).
77 DATA-N PIC 9(2) VALUE ZERO.
01 DATE.
03 DAY PIC 99.
03 F PIC X VALUE “/”.
03 MONTH PIC 99.
03 FILLER PIC X VALUE “/”.
03 YEAR PIC 99.

5.6.EDITING

The data to be printed in a report requires some editing before it can be printed. For example: It is desirable to print a numeric data item but suppressing the leading zeros. If necessary, the sing and decimal point can also be inserted in numeric data.. Editing the normally performed by moving a numeric data item to a field containing special editing characters in its PICTURE Clause.



5.6.1 EDIT CHARACTERS FOR NUMERIC DATA

The following characters can be used in the PICTURE clause to indicate editing.

Z * $ - + CR DB . , B O /

Example:
The following examples illustrate the use of Z editing characters. The character is used to indicate a space character and the character is used to indicate the position of the decimal point.

• Z (Zero suppression) : The edit character Z has the same meaning as that of a 9 in the picture except that the leading zeros in the source data, if any, in the digit position indicated by Z will be suppressed.
• * (Asterisk) : The edit character * (asterisk) is identical to Z except that the leading zeros are replaced by asterisks instead of space characters.
• $ (currency Sign) : A single currency sign can appear at the leftmost position of a picture. In that case the $ is inserted.
• - (minus sign) : A minus sign can appear either at the leftmost or right most position of a picture. If the value is negative, a minus sign will be inserted in the said position. On the other hand, if the item is positive, a space character will be inserted.

Examples
Picture of the Field Numeric value moved to the Edited value
To the Field
ZZ999 04226 b4226
** 999 04678 * 4678
** 999 00052 **052
**999 1 ^ 68 **001
$99999 788 $00788
- 9999 - 454 - 0454

BLANK WHEN ZERO

BLANK WHEN ZERO is an editing clause, which may be used along with a picture. This will set the entire data item to blanks if its value is equal to zero. However, the edit character asterisk (*) may not be used if BLANK WHEN ZERO is specified. When this clause is used to describe a field whose picture contains an asterisk, the compiler ignores it. The syntax of this clause is follows.

BLANK WHEN ZERO
Examples
Picture of the Field Numeric value Moved Edited value
To the Field

ZZZ.99 BLANK WHEN ZERO 2^5 bb2.50
ZZZ.99 BLANK WHEN ZERO 0 bbbbbb
999.99 BLANK WHEN ZERO 0 bbbbbb

5.6.2 Editing of Alphabetic and Alphanumeric Data
Although editing is primarily required for numeric data, limited editing is also possible for alphabetic and alphanumeric data. An alphabetic data item may contain only the B edit character. An alphanumeric data item may contain only o, B and / edit characters.




5.6.4 SPECIAL-NAMES Paragraph
If a currency symbol other than $ required for editing , a one character symbol may be specified in the SPECIAL-NAMES paragraph. This character may not be a digit or letters A to D, L, P, R, S, V, X, Z or special characters blank * - , . ; ( ) + ‘ ‘ / = . the syntax is as follows:



SPECIAL-NAMES.

CURRENCY SIGN IS “character”
DECIMAL POINT IS COMMA.

5.7. CLASSES AND CATEGORIES OF DATA
In order that the class attribute of any data item in COBOL can be determined conclusively, all elementary data items are classified into the following five categories- alphabetic, numeric, numeric edited, alphanumeric and alphanumeric edited. The characters in the picture string determines the category of an elementary data item.
Category Symbols in PICTURE Character string

Alphabetic A B

Numeric 9 p s v

Numeric Edited 9 P V and at least one of the
Editing symbols
B / z O + - * , . CR DB $

Alphanumeric X 9 A
(Must contain at least one X
or a combination of 9 and A)

Alphanumeric Edited X 9 A B O /
(Must contain (i) at least one X
with at least one of B O / or
(ii) at least one A with at least one
O or / )

Procedure Division :- 


STRUCTURE OF THE PROCEDURE DIVISION
The Procedure Division Contains statements, which specify the operations to be performed by the computer. Each of these statements is formed with COBOL words and literals. A Statement always starts with a COBOL verb. The following are examples of COBOL statements.
ADD ALLOWANCE TO BASIC – PAY
IF TOTAL – PAY IS GREATER THAN 1000 GO TO PARA TAX- CALCULATION.

PROCEDURE DIVISION
{ Section- name SECTION.
[ Paragraph-name. [Sentence} …… ] … } …

The following format shows the simplified structure of the PROCEDURE DIVISION when it does not contain sections.
PROCEDURE DIVISION
[Paragraph- name. [Sentence] … ]…

A section can consist of zero, one or more paragraphs constituting its body. All section names must unique and must be different from paragraph names, data names and any other names. All paragraphs within a section must have unique names. In order to make a reference to a non unique paragraph name, it should be qualified as shown below.
Paragraph-name OF Section-name
IN

6.2. DATA MOVEMENT VERB: MOVE
It frequently becomes necessary to move data form one place in the memory to another place. This is done with the help of the MOVE verb. The COBOL statement MOVE A to B means that the value contained in the fieldname A should be transferred to the data field named B. Another example of a MOVE statement may be MOVE 3 to COUNT. In this case the value 3 itself is moved to the field named COUNT. The general form of the MOVE Verb is as follows:
MOVE identifier-1
Literal-1 TO identifier-2 [identifier–3]

6.3. ARITHMETIC VERBS
Most of the problems require some computations to be performed on the input or intermediate data, which are numeric in nature. Arithmetic verbs are used to perform these computations. All these verbs can contain either identifiers or numeric literals or both. In the case of identifiers, they must be elementary numeric fields, and identifiers used after GIVING option can be edited or inedited numeric fields. Some arithmetic verbs in their most elementary forms are discussed below.

6.3.1 ADD
This verb an be used to find the sum of two or mote numbers and to store the sum. The ADD verb takes any note of the following two forms

ADD identifier-1 identifier-2 ……. TO identifier-3 [, identifier-4].....
Literal-1 literal-2

ADD identifier-1 identifier-2 identifier-3 . [GIVING identifier-4 [, identifier-5]…].
Literal-1 literal-2 literal-3

Example
ADD A TO B.
ADD A, B giving C, D, E.




6.3.2 SUBTRACT
This verb is used to subtract one, or the sum of two or more numbers from one or more numbers and to store the result.
The form of the SUBTRACT verb is as follows:

SUBTRACT identifier-1 identifier-2
Numeric- numeric- … FROM identifier-3[ , identifier-4] …
literal-1 literal-2
[ , GIVING identifier-5 [ , identifier-6] …]

Example
a) SUBTRACT A FROM B.
This statement means that the value of A will be subtracted from the value of B and the subtracted result will be stored in B. The decimal point alignment will be done automatically.

6.3.3 MULTIPLY
This statement causes one or more multiplicands to be multiplied by a multiplier and to store the products. The form of the MULTIPLY verb is as follows.

MULTIPLY identifier-1 BY identifier-2 [ , identifier-3] …
numeric-literial-1

[ , GIVING identifier-4 [ , identifer-5] … ]

Example
MULTIPLY A by B.
In this case the value of A and B will be multiplied and the Product will be stored in B. The decimal point position will automatically be taken care of. The old value of B will be lost.




6.3.4 DIVIDE
The Purpose of this verb is to divide one number by another and to store the result. There are several forms of this verb,. One of its forms is as follows.

DIVIDE identifier-1 INTO identifier-2 { , identifier-3} …
numeric-literal-1

[ , GIVING identifier-4 [ , identifier-5] …. ].

Examples
a) DIVIDE 5 INTO A.
If the value of A is 20, then after the execution of this statement the value of A will be 4. The old value of A will be lost.

The Second form of this verb is as follows:

DIVIDE identifier-1 By identifier-2
numeric-literal-1 numeric-literal-2

[ GIVING identifier-3 [ , identifier-4] …]
Examples
DIVIDE A BY 3 GIVING C.

6.4. SEQUENCE CONTROL VERBS
Usually, the statements are executed sequentially one after another. This sequence can be altered with the help of a sequence control verb. There are several sequence control verbs in COBOL. In the present chapter we will discuss only two of these sequence control verbs.

6.4.1 GO TO
This verb is used to unconditionally transfer the control to elsewhere in the program. Its form is as follows
GO To Procedure-name

Example: GO TO ERROR- ROUTINE

6.4.2 STOP
This verb causes the termination of the execution of the object program, its form is STOP RUN

6.5 INPUT AND OUTPUT VERBS
Reading the data into the memory form some input medium (such as punched cards) and writing the results from the memory onto some output medium. (Such as continuous stationary) are of basic importance. The verbs OPEN, READ, WRITE and close are available for such input- Output operations.

6.5.1 OPEN
When a READ or a WRITE operation is performed in a file it must be open. The opening of a file may be done with the help of the OPEN Verb.

OPEN INPUT file name-1 [ , file-name-2] …..
OUT PUT file-name-3 [ , file-name-4] ….

Example-1
OPEN INPUT TRANSACTION, OLD-MASTER OUT PUT NEW-MASTER.

6.5.2 READ
The Purpose of this verb is to make available the next logical record from an input file. It is important to note the meaning of “ Next ” logical record in the above statement.

READ file –name RECORD [ INTO identifier-1 ]
AT END imperative- Statement
Example-1
READ OLD-MASTER AT END MOVE ZERO TO END – OF-RECORDS.



6.5.3 WRITE
The WRITE verb releases a record onto an output file. The syntax of the WRITE statement can be different depending on the output device and medium used.

WRITE record-name [ FROM-identifier-1]

BEFORE ADVANCING INTEGER-1 LINES
AFTER IDENTIFIER-2 LINES

MNEMONIC-NAME
HARDWARE-NAME

6.5.5 ACCEPT
The ACCEPT Statement is used to read low-volume data from the operator’s console, some other hardware device or from the operating system. The general format of the ACCEPT statement is as follows: -

ACCEPT identifier FROM mnemonic-name
DATE
DAY
TIME

Example: ACCEPT FLAG-A FROM CONTROL-DATA.

6.5.6 DISPLAY
The function of the DISPLAY statement is Opposite to that of the ACCEPT Statement. It is used to display low-volume results on the Operator’s Console or some other hardware device. The general format of the DISPLAY Statement is

DISPLAY identifier-1 , identifier-2 ….. [UPON mnemonic-name] literal-1 , literal-2


Example : DISPLAY “RESULT IS ”, THE-RESULT.

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More