Introductory Programs using Simple Screens

We are going to start this course by discussing some of the basics of a COBOL program and then looking at a very simple program that puts a message out on a screen. This is not a program that does anything significant, but it is a working program.

The sample program displays the words HELLO WORLD and two other messages on the screen and keeps them there until the user chooses to end the program. When the user wants to end the program, they will press the ENTER key.

        IDENTIFICATION DIVISION. 
        PROGRAM-ID. HELLO. 
        AUTHOR. GROCER. 
       *ENVIRONMENT DIVISION. 
        DATA DIVISION. 
        WORKING-STORAGE SECTION. 
        01  ANSWER-AREA. 
            05  GET-ANS             PIC X. 
        PROCEDURE DIVISION. 
        MAINLINE. 
            DISPLAY "HELLO WORLD". 
            DISPLAY "THIS IS THE FIRST COBOL PROGRAM". 
            DISPLAY "PRESS ENTER TO END THE PROGRAM". 
            ACCEPT GET-ANS. 
            STOP RUN. 

Simple COBOL Program:

The COBOL program is made up of four DIVISIONS:

        IDENTIFICATION DIVISION 
        ENVIRONMENT DIVISION 
        DATA DIVISION 
        PROCEDURE DIVISION 

These names are reserved words and must be written in the code exactly the way shown.

The IDENTIFICATION DIVISION is used to identify the program by name and to tell who wrote the program. In this program we are giving the program a name under the entry PROGRAM-ID. Note that PROGRAM-ID is a reserved word that has special meaning to COBOL. It tells COBOL that the word that follows is the name of the program. The name must be 8 characters or less and made up of letters and numbers (note there must be at least one letter). I gave the program the name HELLO. I have also chosen to identify the AUTHOR. I have written my last name after the word AUTHOR Be sure to notice the use of periods and hyphens as you look at the COBOL code.

        IDENTIFICATION DIVISION.
        PROGRAM-ID. HELLO.
        AUTHOR. GROCER.

Looking at the structure of the IDENTIFICATION DIVISION, we can see that the code is indented. It starts in column 8 which is also known as margin A. COBOL has margin A which goes from position 8 to position 11 and margin B which starts in position 12 and goes to position 72. It is important to write commands in the correct margin. In this case, IDENTIFICATION DIVISION start in margin A. The PROGRAM-ID and the AUTHOR also start in margin A. In looking at this program, note the use of the periods after the reserved words and after the programmer's entries.

The ENVIRONMENT defines and identifies the environment in which the program will be run. Since I am not using the ENVIRONMENT DIVISION in this program, I have put a * in column 7 to make COBOL treat this line as a comment. I could have eliminated the line, but I wanted to show all 4 divisions.

  
       *ENVIRONMENT DIVISION.

The DATA DIVISION defines the data that is being used by the program, including data that is being read or written, and data that is being stored in work areas. In this example, I need to set up an area in memory to hold the users response to seeing the message on the screen. Data that is set up in work areas in memory is stored in the WORKING-STORAGE SECTION of the DATA DIVISION.

 
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01  ANSWER-AREA.
            05  GET-ANS             PIC X.

Looking at the code above, we can see that the words DATA DIVISION start in margin A (column 8). The words WORKING-STORAGE SECTION also starts in margin A. Note that WORKING-STORAGE has a hyphen which makes it all one word.

When we set up data in the WORKING-STORAGE SECTION, we assume that we will be setting up a lot of things in most programs, so we group the data to make it easier to find. In this example, there is only one piece of data, but I have still chosen to group the data to start establishing good programming habits.

01 ANSWER-AREA is the group name. The 01 tells COBOL that this is a high level data entry. Note that the 01 starts in margin A (column 8). Also notice the required period after the name. ANSWER-AREA is not a reserved word. It is a name that I made up. I could have called it MY-INFO or USER-DATA or any other name I chose (note that a name must contain no spaces, I use the hyphen instead, and that it must be no more than 32 characters in length).

05 GET-ANS is the name of an area in memory where I actually want to store the user's response. Note that again, I made up the name following the rules above. Because I am actually going to store information here, I need to specify the size. The user is going to press a single key so I want one character to store the information in. In specify this by giving the field a PIC X. I could also have written PICTURE X. PIC or PICTURE is the reserved word in COBOL that says I am now going to tell you what the area in memory called GET-ANS is going to look like. X means that the user is going to store alphanumeric data here. Alphanumeric data means any piece of data: letters, numbers, special characters. They type does not matter. Therefore PIC X means reserve one character of memory to hold alphanumeric data and call that one character of memory by the name GET-ANS.

The PROCEDURE DIVISION is where the actual processing is done. The PROCEDURE DIVISION is broken up into PARAGRAPHS each of which contains instructions that will be executed when the program is run. In this program there is only one paragraph. The name of the paragraph is MAINLINE. Again, I made up the name. The name must be made up of letters, numbers and hyphens with no embedded blanks. The maximum size is 32 characters. Note that paragraph names start in margin A (column 8).

        PROCEDURE DIVISION. 
        MAINLINE. 
            DISPLAY "HELLO WORLD". 
            DISPLAY "THIS IS THE FIRST COBOL PROGRAM".
            DISPLAY "PRESS ENTER TO END THE PROGRAM". 
            ACCEPT GET-ANS.
            STOP RUN. 

Inside the paragraph, I code the actual commands that the computer will follow when the program is run or executed. Commands start in margin B (column 12). In this example, I am using three different verbs in the five lines of commands. The commands or verbs are: DISPLAY, ACCEPT and STOP RUN. When a program is executed, it will execute the commands in order, so these five commands will be executed one at a time.

The DISPLAY command displays data on the screen. The actual words that we want to display must be enclosed in quotes. Notice that each command ends in a period. This is optional. However, the last command in the paragraph must end with a period. Note that there are three DISPLAY commands that will display the data on three different lines on the screen. The program will display the lines and then reach the ACCEPT command. The ACCEPT command waits for user input. The name of the field where the answer will be stored, GET-ANS comes directly after the command ACCEPT. When the user presses a key, the key is stored in the one character, alphanumeric GET-ANS field that we defined above. The program will now drop down to the STOP RUN command and exit the program.

Now lets go through the actual steps in preparing the program and running it.

First step: You bring up your COBOL compiler - details of that are in another handout.

Second step: You key in your program. Note that Col 8 is where you start to key the program in. In looking at this handout, it may be slightly different from the version in the lab or your home version, but the differences are minor.

Third step: Now you are ready to compile the program.

Compile takes the COBOL that you have written and turns it into machine language so the program can be run. To do this I click on the Compile/Run option on my menu and then click on Compile Program. This program was clean (there were no errors), so the compile worked. If there had been errors the compiler would let you know and highlight the line with an error in yellow. Errors are things like misspelling of words or missing words that do not let the compiler understand what you are trying to tell it.

Note that the compile also adds line numbers in columns 1-6.

 

Fourth step: Since the program is now compiled (turned into machine language), it is time to execute or run it. To do this, we close the version we wrote and open the machine language or executable version.

The three DISPLAY commands produced the three lines of output shown here. Now the ACCEPT command is waiting for the user response. When the user presses ENTER, processing will continue. The STOP RUN command will be encountered and the following message will be shown indicating that the program ended normally. When the user clicks on OK, they will be back inside COBOL.