COBOL Basics

1 Jun

OK, so let’s get started….

First of all, COBOL is a compiled language, which means that you will need a COBOL compiler available.  The job of the compiler is to convert the easy to read English language of the program into machine language that the computer understands. The same COBOL program can be compiled and run on a variety of machines with only minor variations to the code, but be sure to read the documentation of your particular compiler, as that can vary across platforms. Since there are no COBOL hobbyists, I know you are learning COBOL either in a university setting, or you’re using COBOL on the job. That being said, I’ll assume that your school or employer is providing you with a COBOL compiler.

Any text editor can be used to enter COBOL code, but beware of the coding rules defined below as  COBOL is a very structured language.  Ironically, the greatest advantage of COBOL is also its greatest disadvantage.  COBOL is verbose, very verbose.  The advantage is that it is easy to read a COBOL program and understand the instructions that the program is giving to the computer.   The disadvantage is that coding a COBOL program may become tedious because of the lines of code necessary to perform a task.

Every COBOL program is composed of four DIVISIONS, and each DIVISION may be further divided into SECTIONS.    The four divisions are the IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION, and PROCEDURE DIVISION, which will be defined below.

Have you ever seen a COBOL program?  Here is a COBOL program in its simplest form.  This one is so simple that it does absolutely nothing, but it does contain all the necessary divisions and structure.

IDENTIFICATION DIVISION.

Every COBOL program ever written has the words “IDENTIFICATION DIVISION.” as the very first line of code.    There are absolutely no exceptions to this rule.   Lots of information can be entered into the IDENTIFICATION DIVISION, such as author, date written, date last revised, etc, but I prefer to keep my programs as short as possible without adding any unnecessary lines.  The only mandatory entry in the IDENTIFICATION DIVISION is PROGRAM-ID.    Since the program above does nothing, I have named it appropriately.   There can be no spaces in the program name, so use an underscore (or a hyphen) to separate words.

ENVIRONMENT DIVISION.

The ENVIRONMENT DIVISION is where you name the files that the program will use in execution.   You can also put other information in the ENVIRONMENT DIVISION, such as the source computer, the object computer,  etc,  but, once again, those are optional so I prefer to not clutter my programs with unnecessary lines of code.   If your goal is to improve your typing skills, go ahead and add these lines.  In a later post I’ll show you a sample program that includes all the optional lines of code.   For now, let’s keep it simple, and just tell the computer which files the program will use.

DATA DIVISION.

The DATA DIVISION is where the program stores stuff.  The files that the program will use are defined in this section, and there is also a section called “Working-Storage” where variables are defined.  If you’re not familiar with variables, a variable is simply a storage area where you can move data in and out.  Think of the DATA DIVISION as your data warehouse.

PROCEDURE DIVISION.

The PROCEDURE DIVISION is where all the action takes place.  The PROCEDURE DIVISION is loaded with verbs, because this is where it all happens.    The previous three divisions set the scene, but this is where the plot unfolds.   This division is structured with paragraphs and sentences, and every PROCEDURE DIVISION must have at least one paragraph, as shown in the example above.

CODING RULES

keypunchcard

When COBOL was first invented in 1959, it was originally designed to be punched into cards.   Remember keypunch cards and card readers?  (If you do, you’re at least as old as I am).  The cards were all exactly the same size, and each card could hold a total of 80 characters.   Thankfully, nobody uses punched cards anymore, but their legacy does still exist in every COBOL program.   This legacy gives special meaning to particular column positions:

  • Columns 1 through 6 are designed to hold a sequence number, and column 7 is for special characters only.    For our purposes, we will always leave columns 1 through 6 blank.   Column 7 will be used only to tag a certain line of code as a comment, which is ignored by the program, but useful to the programmer.   (Yes, my boss does like me to comment my code).   We use an asterick (*) in column 7 to tag a comment.
  • Columns 8 through 11 are known to COBOL programmers and compilers as Area A.  Only certain lines of code can start in this area.  You start each DIVISION and SECTION name in this area, as well as WORKING-STORAGE declarations and paragraph names.  I think of Area A as the “headings”.
  • Columns 12 through 72 are referred to as Area B, which is where you put the “details” of the program.    This is also where you start the action statements of your program.
  • Columns 73 through 80 should be left blank, as these lines will be ignored by the compiler.    I guess if you’re really excited about adding additional comments, (and really short ones), you could use these eight columns, but I prefer to leave them blank.

 

 

Leave a Reply

Your email address will not be published.