Saturday, November 16, 2013

COBOL Tutorial Class 1: Introduction


Well, one more COBOL tutorial !  Already there are ample COBOL tutorial / free PDFs in the internet.  Why one more ? Well I guess I want to create a tutorial for the users who don’t have the time to go through an entire book or a PDF. This tutorial is going to be short and quick. Let me split this across multiple posts. Here is the first post.


COBOL stands for COmmon Business Oriented Language. It is the prime programming language in IBM Mainframe computer and was invented in 1958. After that it went through several modification in1974 ,1985 and 2002. As I want my tutorial to be quick and short let us finish the history lesson here and start with the technical details.
Unlike C,C++  or other modern languages COBOL code should start in fixed column in a file.Standard COBOL codes are written in 80 byte record length file. And actual code starts from column 8th.
COBOL program area can be divided into five parts as given below.
  • Column 0–6    : Sequence number area ( can be used to give line number to code)
  • Column 7        :  Indicator field ( type * here to comment a line)
  • Column 8-11    : Area A ( Contains COBOL source code)
  • Column 12-72  : Area B ( Contains COBOL source code)
  • Column 73-80  : Comment / Identification area (Compiler ignore this portion)
COBOL program contains four divisions. Each division is then sub-divided into sections and sections can contain paragraphs and that contain sentences.
As told  four divisions are as stated below. We will learn more on these later.
  1. IDENTIFICATION DIVISION : This is used to provide basic information such as program name ,author name , date written etc. It should minimum have PROGRAM-ID coded.
  2. ENVIRONMENT DIVISION : This is used to specify environment in which the program runs.
  3. DATA DIVISION : This portion is used for declaring all the COBOL variables and defining file definition. Three major sections in data division are FILE SECTION, WORKING-STORAGE SECTION and LINKAGE SECTION.
  4. PROCEDURE DIVISION : The actual code logic is written here. This is the main part of a program.
Let me show a simple hello world program in COBOL.

000100 IDENTIFICATION DIVISION.                 
000200 PROGRAM-ID.    HELLO1.                       
000300 DATE-WRITTEN.  NOV 16, 2012.           000400*                             
000500* THIS PROGRAM WILL DISPLAY HELLO WORLD         
000700*                                         
000800 ENVIRONMENT DIVISION.   
000900*                             
000100 DATA DIVISION.                     
000110*   
000120 PROCEDURE DIVISION.
000130*
000140 0000-MAINLINE.
000150          DISPLAY  "Hello world".
000160          STOP RUN.


The program name is hello1. Always do keep the file name ( basically PDS member name assuming you are coding in IBM Mainframe ) and program-ID same. The environment and data division are kept empty as we don’t need to code anything here for such a simple program. Division name and paragraph name ( PROGRAM-ID and 0000-Mainline) starts in column 8 ( area A) and display and STOP RUN sentence starts in column 12 ( area B). STOP RUN sentence causes the program to terminate when the sentence is executed. Comments are coded by typing * in column seven.

No comments:

Post a Comment