Introduction to COBOL

COBOL - COmmon Business Oriented Language is a structured Programming Language which works on these 3 principles Sequence, Selection and Iteration.

A structured Programming Language had the following advantage,

  • They have 1 starting point and 1 ending point.
  • Makes debugging easier.
  • Consumes optimum time.
  • Well organized transfer of control to different parts of the program.

COBOL Coding sheet

Like JCL, COBOL also has a coding format to follow with the 80 byte coding sheet.

cobol-coding-sheet

Sequence field for line numbers
Indicator field ‘/’ or ‘*’ or ‘-’
Margin A & Margin B Program Statements
Identification field Source listing

Structure of COBOL program:

A COBOL program is divided in to a structure and each structure will contain another structure within it. To explain it better, check this picture

cobol-program-structure

COBOL program has several DIVISIONS. With in each division there will be several SECTIONS and with in each sections, there will be several PARAGRAPHS. With in each paragraphs, there will be COBOL statements which are comprised of COBOL verbs. 

Things to note in COBOL program structure:

  • All headings, statements, declarations of a COBOL program end with a period(full stop).
  • The DIVISIONS are Pre-defined by the language which begins in MARGIN A.
  • SECTIONS are Pre-defined by the language and it can also be defined by a user. Sections also begins in MARGIN A. Example
    • Select-records SECTION. - User defined
    • FILE SECTION. – Pre-defined Section by language
  • PARAGRAPHS are Pre-defined by the language and also custom defined by the user. Begins in MARGIN A. Example,
    • 100-START-ROUTINE. - User defined
    • PROGRAM-ID.  – Pre-defined Section by language
  • STATEMENTS are stand alone instructions which begins in MARGIN B. Example,
    • ADD REC-1 to REC-2
  • SENTENCES are a single statement or a group of statements ending with a period.
  • VERBS are the words used to define a action. Example
    • ADD

COBOL Verbs:

COBOL words have some reserved keywords and most are user-defined.

A COBOL word must be formed of the following characters

0 – 9 or A – Z or hyphen

The character set in COBOL are

COBOL character set has 52 characters

0 – 9 Numerals
A – Z English Letters, Space or Blank
+ - * / = $ or £ , ; : . " ( ) < > Special characters

Identifiers:

COBOL identifiers are same as variables which provides reference to the storage space in the memory.

The identifier naming rules are:

  • Maximum length of 30 characters.
  • The characters can be 0 – 9, A – Z or hyphen. Atleast one alphabet should be present.
  • Should not begin or end with hyphen.
  • No blanks and special characters allowed.
  • Should not be COBOL reserved words.

Examples of valid datanames or identifiers or variables

EMP-NUM
EMP123
123EMP

Examples of Invalid datanames or identifiers or variables

-EMP-NUM