0 Flares Twitter 0 Facebook 0 Filament.io 0 Flares ×

Introduction

Today, we are going to start off coding up some routines (also known as looping in Object-Oriented Programming) with the COBOL programming language. If you’re a programmer having a background on COBOL, you might think why is looping made complicating? If you’re new to this well, considering that this is a very old language, just think of this as a challenge and a way to make you feel a little mind-tingling (LITERALLY!). I’m going to show you how it works in a common scenario wherein it asks for  user input. Just so you know, I’m not using any high-level IDE’s. Just the old notepad(++) and cmd (Hooray for old school!).

 

Pre-requisites:

Things you’ll need to you started

COBOL Compiler –  I’m currently using COBOL 74 (Yup yup  I know its old and yata yata yata but its what I currently use for teaching purposes).

DosBoxThis is actually optional. If your COBOL compiler compiles flawlessly on the command prompt you don’t need this. The reason why I’m using this is because: 

  • I’m using a x64(sixty-four bit) machine.
  • To ensure old programs such as this can run flawlessly
  • As much as possible, to avoid encountering the legendary “Null Pointer Assignment Error“.

Try compiling it in your cmd but if any of my reasons were one of the problems, then go ahead for DosBox.

Text Editor – For COBOL, I’d personally prefer notepad++ because this can detect your spacing whether its real space or just a tab space (Take note that both are not the same as they have different ASCII values and yup, they do matter here).

 

Looks like thats about it. Lets get started shall we?

1.) Writing the 4 Divisions (With a little declaration)

On this part, we will be writing the 4 divisions of COBOL namely Identification, Environment, Data, and Procedure Division (I’ll go into a little detail with them later). Whenever writing a COBOL program, always remember that the rule of the thumb is Declarations such as Divisions and Routines are always declared in Area A. Area A codes are declared from 8 – 12 spaces and Area B from 13 – 72. Let’s try to write it down:

Screen Shot 2015-01-27 at 1.21.44 AM

 

Raw Code:

        IDENTIFICATION DIVISION.
        PROGRAM-ID. SAMPLE.
        ENVIRONMENT DIVISION.
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 CHR PIC X.
        01 ANS PIC X VALUE 'Y'.
        PROCEDURE DIVISION.

Here’s the explanation to the code per line:

Note: Every line of code in COBOL is terminated by using the character . <- Dot

  • (1) Identification Division – The first Division declared in COBOL. Used to identify the program to the computer and provide documentation. (You can click here to see what do I mean by documentation)
  • (2) Program IdUsed to define the program name. Take note that this is only limited to 8 characters and is the only required paragraph in the identification division (Yes, there are other paragraphs for this division). This should always be the same as the file name. Beside it is the name of the program in our case named, SAMPLE. If you are familiar with other OOP languages, this is similar to your main Class name (As class names define the name of your file).

Here’s a little bit of translation to a powerful language for the Identification Division:

Identification_Division()
{
    Program_Id = "Sample";
}
  • (3) Environment Division The second Division in a COBOL Program. This Division tells what computer system or hardware shall the program interact. In our case, we don’t need any of those for now so we leave this division with no configuration.
  • (4) Data Division The third division in a COBOL program.  This Division defines and describes fields, records, and files in storage. In OOP cases, this is the part where you declare variables, assign values and instantiate objects.
  • (5) Working Storage Section This paragraph is part of the Data Division. This reserves storage for fields that are not part of input or output but are required for processing such as PIC’s (variables)
  • (6) 01 CHR PIC X. – I’ll be explaining this per word/number.
    • 01 – Let me first explain what does a “01” do there. In COBOL programs, whenever defining variables, you should always define a Level Number. Level numbers serves as a hierarchy in records. Records coded in level 1 are the highest level of data in a file. The next question would be why do you need it? Simple. Then again, it serves as a hierarchy. It is possible to declare hierarchy of records such as this:
        01 EMPLOYEE-RECORDS.
            02 NAME PIC X(50).
            02 SALARY PIC 99999.

So whenever I call EMPLOYEE-RECORDS, it throws all records within its subordinates. In Object-Oriented Programming, its similar to declaring an object like this:

Object employee_records = new Object();
    employee_records.name = "Juan Dela Cruz";
    employee_records.salary = "80000";

But right now, I won’t be demonstrating such. Let’s just stick to the basics for now.

    • CHR – This is the name of your variable. This is defined by the programmer.
    • PIC X – This is the data type of the variable. In COBOL, declarations are always in PIC. There are 3 types of fields. X – Alphanumeric (both letters and numbers), 9 – Numeric, and A – Alphabetic. The size of the field depends on how many X’s, 9’s or A’s I placed. So if I do PIC XXX, it can store a maximum of 3 digits/characters. I can also do PIC X(3) to make it simple.
  • (7) 01 ANS PIC X VALUE ‘Y’. – This is just the same as above but this time, it only adds a value of “Y”. In programming, its similar to declaring var ans = ‘Y’;
  • (8) PROCEDURE DIVISION – The fourth division in a COBOL program. This is where we place the instructions necessary from processing the input to come up with the required output (For short, the logic). It is composed of sentences or statements, grouped by paragraph names.  

 

2.) Writing the Paragraph names and Logic Processing

Now for the interesting part, writing the REAL code. from here, we create the logic for the program with the use of paragraphs for loop-statements and later on use inputs from the user. Let’s write down our first Paragraph:

cobol scrn

 

Raw Code:

       HEAD-RTN.
           PERFORM MAIN-RTN UNTIL ANS = 'N' OR ANS = 'n'.
           PERFORM END-RTN.

Here’s the explanation to the code:

 

  • HEAD-RTN This is our paragraph named “HEAD-RTN” (Short for head routine). This is defined by the programmer. In Object-Oriented Programming, this is defined as a function.
  • PERFORM MAIN-RTN UNTIL ANS = ‘N’ OR ANS = ‘n’ – This is written in Area B as this now indicates the logic. Let’s break it down to bits and pieces shall we?
    • PERFORM <INSERT ROUTINE HERE>UNTIL – serves as the basic iterative statement in COBOL, performs the statements until a specific condition is met. This is similar to a while loop in OOP.
    • MAIN-RTN – Uhhhhmm… the routine name and then again, this is defined by the programmer. This is not yet defined for we’ll do that after this.
    • ANS = ‘N’ OR ANS = ‘n’ – This is the condition where in if it gets satisfied, the routine stops. In this case, if the value of ANS is either ‘N‘ or ‘n‘, it breaks the loop.
  • PERFORM END-RTN – Basically the same as above but performs END-RTN with no conditions. Then again, it’s not yet defined so calm down.
  • For the logic flow, the first perform statement loops on until it meets the condition followed by the second perform. Converting this to OOP would be similar to this:
Head_Routine()
{
    while(Main_Routine() == 'N' || Main_Routine() == 'n'){ }
    End_Routine();
}

Let’s write our next paragraph, “MAIN-RTN“:

cobol scrn2

Raw Code:

       MAIN-RTN.
            DISPLAY (5 5) 'HELLO WORLD!'.
            DISPLAY (7 5) 'ENTER ANY CHARACTER:'.
            ACCEPT (7 28) CHR.
            DISPLAY (13 8) 'YOU ENTERED ', CHR.
            DISPLAY (14 9) 'AGAIN?(Y/N)'.
            ACCEPT (15 9) ANS.

 

Here’s the explanation to the code:

  • DISPLAY (Y, X) <Message> –  Used to display the message. Y and X are the coordinates it will appear on the interface. Placing X,Y coordinates are optional. I only placed it to display the message on the middle. If ever you didn’t, don’t worry for the message will be displayed on the top-left corner.
  • ACCEPT <PIC> – Used to get input from the user. The PIC beside it will be the one used to pass on data from the user.

Now let’s write the last routine, “END-RTN”:

cobol scrn3

 

Raw Code:

        END-RTN.
             DISPLAY (16 5) 'BYE!'.
             DISPLAY (17 5) 'SEE YOU Later!'.
             STOP RUN.

 

Well from that point I guess you don’t need an explanation to this last routine. The only thing new here is the STOP RUN and all it does is to simply stop the program from running.

Let’s compile our COBOL program. Save the file as SAMPLE.COB to your directory where you have your COBOL compilers. To run the program, assuming you have your cmd on the compiler’s directory or had your DOSBox mounted on your compiler’s directory, just simply type:

COBOL SAMPLE.COB; <Enter>

RUNCOB SAMPLE <Enter>

Take note that you have to place a semi-colon on the first compilation(line) because if you don’t, it will still ask for the object and listing files. Though it may not affect your program, its faster to execute without getting prompted.

The program should look like this:

Screen Shot 2015-01-29 at 1.50.05 AM

 

Debugging Tips

You might encounter some weird errors. Most likely it could occur on spacings, misspelled words and unterminated lines. Here are some tips to avoid some of those problems:

  • If your using Notepad++, you can check your spacings by going to View > Show Symbol > Show White Spacing and TABScreen Shot 2015-01-29 at 1.54.47 AMFrom there, you can check whether your spacing was spaces or tabs. COBOL is extremely sensitive when it comes to spacing so make sure that whenever making a new line, your spacings are encoded on space, not tabs. Below is knowing the difference between the 2 in Notepad++Screen Shot 2015-01-29 at 2.01.53 AM
  • If you encounter a NULL Pointer Assignment error while compiling (Not sure on the exact term), its a sign you have to use DOSBox. The reason is because not all OS have the same builds plus as a new version gets released, older systems get depreciated. DOSBox is there to support those old systems.
  • To easily read COBOL codes, Notepad++ has a feature to distinguish a reserved word to a user defined ROUTINE/PIC. Go to Language > C > COBOL. Screen Shot 2015-01-29 at 2.13.48 AM
  • Always double-check your line terminators and your spelling

 

Debriefing

In the end, here’s what your code should look like:

 Screen Shot 2015-01-29 at 2.20.16 AM

 

Raw Code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SAMPLE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 CHR PIC X.
       01 ANS PIC X VALUE 'Y'.
       PROCEDURE DIVISION.
       HEAD-RTN.
           PERFORM MAIN-RTN UNTIL ANS = 'N' OR ANS = 'n'.
           PERFORM END-RTN.
       MAIN-RTN.
           DISPLAY (5 5) 'HELLO WORLD!'.
           DISPLAY (7 5) 'ENTER ANY CHARACTER:'.
           ACCEPT (7 28) CHR.
           DISPLAY (13 8) 'YOU ENTERED ', CHR.
           DISPLAY (14 9) 'AGAIN?(Y/N)'.
           ACCEPT (15 9) ANS.
       END-RTN.
           DISPLAY (16 5) 'BYE!'.
           DISPLAY (17 5) 'SEE YOU Later!'.
           STOP RUN.

Still getting errors? Can’t copy-paste in the site well? Don’t worry, click here for the real raw file (Deployed on GitHub).

Thanks and hope you enjoyed my tutorials!

Leave a Reply

Your email address will not be published. Required fields are marked *