Mainframes 360
The one stop destination for System Z professionals

Wednesday, March 5, 2014

Cobol – An Introduction

COBOL is a language for business applications

COBOL (COmmon Business Oriented Language) is a high-level programming language suited to develop business applications. COBOL programs are employed in business processes likePayroll, Accounting, Inventory Management, Billing, Reservation Systems etc. Applications built in COBOL are in wide-spread use across the globe.

The way COBOL works

Computers really understand only one language : machine code, a binary stream of 0s and 1s. You must convert your COBOL code into machine code with the aid of a compiler.

Anatomy of a COBOL program

When z/OS runs your COBOL program, it looks for a specially coded DIVISION that looks exactly like:
  PROCEDURE DIVISION.
your code goes here
STOP RUN.

The zOS runs everything in the PROCEDURE DIVISION of your COBOL program and stops execution at STOP RUN. In COBOL, you put instructions or code inside the PROCEDURE DIVISION, followed by STOP RUN.


Code Structure in COBOL

COBOL programs have an IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION and a PROCEDURE DIVISION. DIVISION's can have SECTION's and SECTION's have paragraphs. A paragraph is a block of code that has COBOL statements, we'd like to call them sentences.

You put declarative statements in the IDENTIFICATION DIVISION, ENVIRONMENT DIVISION and DATA DIVISION. You write instructions or executable statements in the PROCEDURE DIVISION. PROCEDURE DIVISION is where the ball starts rolling.

The first part of the COBOL program is the IDENTIFICATION DIVISION. This division has information that helps identify the program-name, author, date it was written etc. The ENVIRONMENT DIVISION has information on the platform/environment, the program would run. The DATA DIVISION declares variables and data structures. The PROCEDURE DIVISION contains executable code.
       IDENTIFICATION DIVISION.
  PROGRAM-ID. MYPROG01.
 
  ENVIRONMENT DIVISION.
 
  DATA DIVISION.
 
  PROCEDURE DIVISION.
  0000-MAIN.
  DISPLAY 'I RULE!'
  STOP RUN.

Look at the lean program I've written above. The IDENTIFICATION DIVISION is the first division in every COBOL program. PROGRAM-ID is a paragraph in the IDENTIFICATION DIVISION. MYPROG01 is a statement inside PROGRAM-ID. The z/OS will identify this program as MYPROG01. Next, the ENVIRONMENT DIVISION must describe the system or platform for the program. The DATA DIVISION declares data-items. Don't worry about these right now, this is just to get you started!

I've defined a paragraph 0000-MAIN in PROCEDURE DIVISION. The DISPLAY statement in COBOL displays text strings on the terminal. 'I RULE!' is displayed on the screen. STOP RUN indicates the end of the COBOL program and execution stops.

What can you say in the PROCEDURE DIVISION?

Once you're inside the PROCEDURE DIVISION, the fun begins! Like in most programming languages, you can do normal things like assignments, arithmetic. You can do something under a condition - IF/ELSE tests. You can do something again and again - Loop or iterate.


COBOL is verbose and has English-like expressions to depict logic(Its inventors envisioned that perhaps COBOL could be read by programmers and managers alike). MOVE is used for assignments : MOVE 'JOHN RAMBO' TO FULL-NAME, MOVE 10000 TO PRINCIPAL-SUM, MOVE TOTAL-COST TO RPT-FIELD-1. COMPUTE helps with writing and evaluating arithmetic expressions. Say, you had to find the volume of sphere. Just code COMPUTE VOLUME = (4/3) * (3.14159) * (RADIUS ** 3).

COBOL is a structured programming language. It employs a top-down design model. The whole program logic is divided into smaller sections and paragraphs. For example, 3000-PROCESS-FILE logic can be divided into 3100-OPEN-FILE, 3200-READ-FILE and 3300-CLOSE-FILE paragraphs. This makes it highly modular. You usually map-out similar functions or logic to its own separate paragraph. The logic for reading from a file can be encapsulated in a 3200-READ-FILE para. At any point in the program, if a file is to be read, you can just invoke or PERFORM 3200-READ-FILE.


Looping and looping and...

COBOL has a standard looping construct PERFORM UNTIL.. do-something END-PERFORM. We'll talk about loops at length, later. The syntax is so simple - you're probably asleep already. Keep doing everything inside the PERFORM .. END-PERFORM block over and over. Whatever it is that you want repeat, has to be inside the block.

The key to a loop is a conditional-test. In COBOL, a conditional-test is an expression that returns a boolean value, in other words a TRUE or FALSE. If you say something like, "Keep juggling, until no more juggling pieces are there", this is a clear boolean test. As long as the conditional-test is FALSE, keep juggling. When no more juggling balls are left, you stop.
           PERFORM UNTIL NO-MORE-JUGGLING-PIECES
  PERFORM KEEP-JUGGLING
  END-PERFORM

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.