COBOL Tutorial: What is, History, Install, Program Example
What is COBOL?
COBOL is a programming language that is mainly focused on solving a business problem. Full form of COBOL is Common Business-Oriented Language. It is primarily used in company and government business, finance, and administrative systems. This language also used as a solution to many data processing problems.
It is developed by CODASYL (Conference on Data Systems Languages). It is used as a language in the Mainframe System.
Step 3) In the next screen, click on the Next button.
Step 4) In Public License agreement screen,
Select the radio button accept the term the Licence Agreement
Click on Next Button
Step 5) In the next screen, click on Install button.
Step 6) Click on Install button
Step 7) Click on the finish button.
Step 8) In the start menu, click on the Hercules CMD prompt.
Step 9) You can see the command prompt.
Structure of the COBOL program
The organization of a COBOL program is hierarchical. It's not necessarily needed for all of the components to be present for the hierarchical relationship to exist.
Here, are various components of the COBOL program:
A division is a block of code, which usually contains one or more sections. It starts where the division name is encountered. It ends with the beginning of the next division.
Sections are the logical subdivision of the program logic. It is the collection of paragraphs.
Paragraphs are the portion of a selection which is a user-defined or predefined name which should be followed by a period. This section includes zero or more sentences.
Sentences are a combination of single or multiple statements. They should occur only in the procedure division. A sentence in COBOL language must end with a period(.).
These are the meaningful COBOL statements which perform some processing.
These are the lowest in the hierarchy which cannot be divided.
Types of Divisions
Various divisions in COBOL program structure are as follows:
The programmer and compiler can use this division to recognize the program.
The only compulsory division is Program ID.
It identifies the name of the program which contains characters.
It helps you to identify input and output files to the program.
This section gives information regarding the system on which the program is written and run.
This section has two paragraphs:
Source computer – Complete program
Object computer – Execute the program.
This section gives detail related to files which are used in the program.
It has four sections:
File section - Allows you to define the record structure of the file.
Working storage section: This section helps you to declare the temp variables and file structures.
Local-storage section: This section is the same as the local working storage section. The key difference between the two is that variables must be initialized every time when the program starts execution.
Linkage section: This section describes the data names received from the external storage.
This division is used for including the logic of the program.
Procedure division also includes executable statements which are defined in the data division.
COBOL coding rules
Here, are some most important coding rules while working with COBOL:
The first six character positions are allocated for sequence numbers.
The seventh character position is reserved for the continuation character, or for an asterisk which denotes a comment line.
The actual real program text starts with column 8.
The fourth positions from 8 to 11 are known as Area A, and positions from 12 to 72 are called Area B.
The standard form of Cobol program:
There are 80 characters on each line.
Columns(l-6): It indicates the sequence
Column 7: This position contains the indicator^,/,-)
Columns(8-ll): It denotes the Area A.
Columns(12-72): It denotes the Area B.
Columns(72-80): Use for Identification.
Program Syntax Rules of COBOL
Here, some important syntax rules of COBOL:
COBOL has syntax, which is very similar to the English language, that was designed to be self-documenting and very readable.
Example: y = x; is represented with: MOVE x TO y
It is not a case sensitive language.
It has more than 300 reserved words
Complex conditions can be "abbreviated" by removing repeated variables and conditions. For example:
IS a GREATER THAN x AND y GREATER THAN OR EQUALS To
It lacks big size standard library, as it has only 43 statements, 87 functions, and just one class.
Variables in COBOL
In COBOL variable is a named location in memory into which a program can put data and from which it can retrieve data. A data-name or identity. It is the name used to identify the area of memory that is reserved for the variable.
Variables must be described in terms of their size and type. Every variable used in a COBOL program should have encryption in the DATA DIVISION.
Rules for declaring variables in COBOL:
Variables should be declared in working-storage section Variables can be arrays, records, file descriptor records, etc.
The name of a variable may have alphanumeric, with its first character as an alphabet.
Only allowed special character used and can have the only hyphen in it which must be embedded
9 represents only single-digit number.
If Else Statements
In COBOL, If else statement is used for conditional branching
The relational operators and some spelled out phrases can be used
If x < y - this can be written as if x is less than y
<, >, =,>=,<= are the available relational operators
NOT, AND, OR are logical operators used in COBOL
Greater than, less than, equal to are the spelled out phrases
If x is less than y then
Do the first action
Do the second action
To check an alpha number variable for what type of data it holds, we can use if x is number, if x is alphabetic, conditions. It is based on T or F value.
You can use if-then-else if for multi-level conditions
When 1 perform action1
When 2 perform action2
When 3 perform action3
When others perform action4
If no condition is matched, then the other clause is executed. This is quite similar to the switch statements in other languages.