Back to first page...

Our First Simple COBOL Program

Now, we shall write a very simple program which performs some useless operations on useless data. Please note that the objective is to demonstrate how COBOL code is writen, which are the essesntial parts of a COBOL program, how a COBOL is program is compiled and run.

Our program objective is to tell the user whether a number he/she enters is add or even. (Big deal! )

Suppose we want the message
   
 "Enter an integer :" 
to appear in the middle of the screen; and when the user enters the number and hits the ENTER key, we want to display "Even" or "Odd" accordingly just next to the entered number.

To key-in the source of our program, we shall need a text editor. Actually, any editor will do. Even the Notepad utility of MS-Windows... I would recommend you to use the MS-DOS standard text editor which you can invoke with the EDIT command. Before you start creating your source file, do not forget to create your own sub-directory at a convenient place on the PC disks and change your default directory to this working space.


    MKDIR \CUAYFER
    CD \CUAYFER
    EDIT PROG1.CBL
Please note that the file extension for our source file is CBL. This is a convention of the RM-COBOL compiler that we shall be using and not necessarily same for all COBOL compilers.

Since every COBOL program must have an identification division, you should start with something like this :
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MY-FIRST-COBOL-PROGRAM.

Please note that these lines SHOULD start on column 8. You can see the cursor position at the right bottom corner of the editor screen.

Please also note the "." characters, especially at the ends of the command lines. In very near future, you shall be hunting extra and/or missing "."s in your programs. :)

The next division we should enter is the ENVIRONMENT division :

       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. ANY-MS-DOS-PC.
       OBJECT-COMPUTER. ANY-MS-DOS-PC.
 
Please also note the dash "-" characters even in commentary fields (MY-FIRST-COBOL-PROGRAM, ANY-MS-DOS-PC). Although some compilers will let you use blanks in these fields; RM-COBOL won't!

Since we shall not be using any disk/tape files and/or Printers in our program, we do not need to have an INPUT-OUTPUT SECTION. Although we shall be using Input-Output statements, we do not need any special declarations for the CONSOLE CRT SCREEN I/O.

Next, the DATA DIVISON! Since we shall not use any disk/tape files or printers, we need not use a FILE SECTION. Therefore we proceed right next to the WORKING-STORAGE SECTION.

In the working-storage section, we have to declare all memory variables that we shall use in our program. Normally, you wouldn't know all the variables and their types that you will use in the pgm. While you are writing your code, whenever you mention a new variable, you must come back to this section and declare it!

For our simple program, we can decide NOW that we should use at least two emory variables; one for the number that user will enter and the other for the remainder when we divide this number by two.

Lets choose the variable names NUMBER-ENTERED and REMNANT for the remainder. The variable name REMAINDER would not be a good choice, BECAUSE IT IS A RESERVED WORD!.

Click here for a short list of COBOL reserved words. Please note that this is not a complete list and further, the list will be different for various COBOL implementations.

Next, we should decide the types and length of thse variables. In this example they certainly will be numercial; the input value can be limited to 9 digits, and REMNANT can be be a single digit number.

The working-storage declarations for these SCALAR variables can be :

       77   NUMBER-ENTERED     PIC 9(9).
       77   REMNANT            PIC 9.
Note that the 77's start at column 8, the variable names start at column 12 or beyond col 12!

Now are ready to proceed to PROCEDURE DIVISION. This is the part of the COBOL code where we shall do our actual work.

All COBOL procedure divisions start with a PARAGRAPH NAME at column 8. Any name will do but it is customary to use the name MAIN for the first paragraph. By the way, a COBOL paragraph is a group of procedure division statements that are collected under a common paragraph name and make some sense together with the other statements in the paragraph. The rules related to paragraphs are very loose except the first one.

An analogy to COBOL paragraphs are LABELS in other programming languages. With the use of various sequence control statements (like GO TO, PERFORM), you can branch the execution sequence to paragraphs, or in other words, to first statements of paragraphs.

Our PROCEDURE DIVISION can go like this :
      PROCEDURE DIVISION.
       MAIN.          
      *     
           DISPLAY "Enter an integer :" LINE 12 POSITION 20 ERASE.
           DISPLAY "Enter a zero to terminate the program" LINE 24 POSITION 20.
           ACCEPT NUMBER-ENTERED LINE 12 POSITION 40.
           IF NUMBER-ENTERED = ZERO THEN
              DISPLAY "Normal Termination!" LINE 1 POSITION 1 ERASE
              STOP RUN.
      *
           DIVIDE 2 INTO NUMBER-ENTERED GIVING USELESS-RESULT REMAINDER REMNANT.
Ooops! We had to introduce a new variable "USELESS-RESULT" which will hold the quotient of division. We do need the value but the DIVIDE statement needs it. We have to go back to working-storage section and declare it too!

The modified declaration statement group would look like :
       77   NUMBER-ENTERED     PIC 9(9).
       77   REMNANT            PIC 9.
       77   USELESS-RESULT     PIC 9(9).
Now back to our procedure...

      PROCEDURE DIVISION.
       MAIN.          
      *     
           DISPLAY "Enter an integer :" LINE 12 POSITION 20 ERASE.
           DISPLAY "Enter a zero to terminate the program" LINE 24 POSITION 20.
           ACCEPT NUMBER-ENTERED LINE 12 POSITION 40.
           IF NUMBER-ENTERED = ZERO THEN
              DISPLAY "Normal Termination!" LINE 1 POSITION 1 ERASE
              STOP RUN.
      *
           DIVIDE 2 INTO NUMBER-ENTERED GIVING USELESS-RESULT REMAINDER REMNANT.
      *
           IF REMNANT = 1 THEN
              DISPLAY "Odd" LINE 12 POSITION 52
              ELSE
              DISPLAY "Even" LINE 12 POSITION 52.
      *
           DISPLAY "Hit ENTER key to continue..." LINE 24 POSITION 20.
           ACCEPT XDUMMY.
           GO MAIN.
See? Another variable.... This time we shall add the statement
       77    XDUMMY    PIC X.
We shouldn't use PIC 9 for this variable because the user might enter a letter or a special character just before hitting the ENTER key and cause trouble. Obviously the letter "A" cannot be read into a numeric variable.

Now, the complete program should look somewhat like :
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. ANY-MS-DOS-PC.
       OBJECT-COMPUTER. ANY-MS-DOS-PC.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       77   NUMBER-ENTERED     PIC 9(9).
       77   REMNANT            PIC 9.
       77   USELESS-RESULT     PIC 9(9).
       77   XDUMMY             PIC X.
      *
       PROCEDURE DIVISION.
       MAIN.
      *     
           DISPLAY "Enter an integer :"  LINE 12 POSITION 20 ERASE.
           DISPLAY "Enter a zero to terminate the program" LINE 24 POSITION 20.
           ACCEPT NUMBER-ENTERED LINE 12 POSITION 40.
           IF NUMBER-ENTERED = ZERO THEN
              DISPLAY "Normal Termination!" LINE 1 POSITION 1 ERASE
              STOP RUN.
      *
           DIVIDE 2 INTO NUMBER-ENTERED GIVING USELESS-RESULT REMAINDER REMNANT.
      *
           IF REMNANT = 1 THEN
              DISPLAY "Odd" LINE 12 POSITION 52
              ELSE
              DISPLAY "Even" LINE 12 POSITION 52.
      *
           DISPLAY "Hit ENTER key to continue..." LINE 24 POSITION 20.
           ACCEPT XDUMMY.
           GO MAIN.

Now, let's compile our program and run it!

The MS-DOS command to invoke an RM-COBOL compilation is :
      RMCOBOL PROG1
You do not need to specify the extension of the source file as long as it is .CBL.

Now try it!

Too many error messages! Right?

You couldn't even follow the long list of messages... Try the MS-DOS command
      RMCOBOL PROG1 | MORE
to get the long message list with a pause between screen pages.

Can you guess why you had so manu errors even in a small program like this? Let me tell you! You typed beyond column 72 and the compiler simply ignored all characters you typed at columns 73, 74 etc..

No problem! Go back to the editor and insert new line characters into statements so that the new source looks something like :

       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. ANY-MS-DOS-PC.
       OBJECT-COMPUTER. ANY-MS-DOS-PC.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       77   NUMBER-ENTERED     PIC 9(9).
       77   REMNANT            PIC 9.
       77   USELESS-RESULT     PIC 9(9).
       77   XDUMMY             PIC X.
      *
       PROCEDURE DIVISION.
       MAIN.
      *     
           DISPLAY "Enter an integer :"  LINE 12 
                                         POSITION 20 ERASE.
           DISPLAY "Enter a zero to terminate the program" 
                                         LINE 24 POSITION 20.
           ACCEPT NUMBER-ENTERED LINE 12 POSITION 40.
           IF NUMBER-ENTERED = ZERO THEN
              DISPLAY "Normal Termination!" LINE 1 POSITION 1 ERASE
              STOP RUN.
      *
           DIVIDE 2 INTO NUMBER-ENTERED GIVING 
                  USELESS-RESULT REMAINDER REMNANT.
      *
           IF REMNANT = 1 THEN
              DISPLAY "Odd"  LINE 12 POSITION 52
              ELSE
              DISPLAY "Even" LINE 12 POSITION 52.
      *
           DISPLAY "Hit ENTER key to continue..." 
                             LINE 24 POSITION 20.
           ACCEPT XDUMMY.
           GO MAIN.

IMPORTANT!

You should NOT divide verbs, variable names etc when you insert new lines into your source. Also you should not put any "." at the end of lines. "."s are used to indicate end of statements, NOT end of lines!

Try to compile your program again... It should report "0 Errors".

If you can compile without any errors; you are ready to execute your program.

Type
      RUNCOBOL PROG1
What happens is this : The compiler produces a file called PROG1.COB into your working directory and this is the "executable" (or rather "runnable") version of your program. With the help of RUNCOBOL utility (actually is called "COBOL RUNTIME") you "sort of execute" your program.

I use these vague terms "sort of..", "rather runnable" because unlike most compilers, RM-COBOL does NOT translate your source into machine code! It translates your program into an "intermediate code" and then INTERPRETS this code with the help of its runtime; namely RUNCOBOL...

Anyway, when you run your program you should get a feeling that it is running correctly except that the screen is a mess...

The reason is straightforward! We did not clear the screen before we started displaying messages on the screen. Try adding the statement
           DISPLAY " " LINE 1 POSITION 1 ERASE EOS.
This statement will display a blank character on the first row, first column of the screen and then EOS (erase to end of screen).

Now, try running your program again. It looks much better now, doesn't it?

The only problem is; you cannot see the "Normal Termination..." message when you enter a zero... Can you guess the reason? Please try to correct this defect! It can be good quiz question! :)

Now you can play around with this program and put some fancy features in it. For example, try to disable "beep" signal that you hear everytime a new number is ACCEPT'ed. You can also try to make the "Hit ENTER key to continue..." message blinking so that it gets the user's attention.

I shall appreciate if you MAIL me the source of the revised program. All decent jokes and funny ideas are welcome!


Back to first page...