Back to first page...

THE PROCEDURE DIVISION

This is the division where the COBOL programmer actually codes his/her executable statements or, in otherwords the algorithm .

The COBOL statements that can be used in the PROCEDURE DIVISION are too many to be covered in a single chapter. In fact, most of these statements will be topics in our course.

Generally speaking, COBOL statements can be classified into : (like all structured programming languages). By the way; can you name an UN-STRUCTURED programming language?

ASSIGNMENT STATEMENTS & EXPRESSIONS

All programming languages make use of memory variables and other data items that represent various type of data and naturally need to have assignment statements.

A very simple form of a COBOL assignment statement is :

VARIABLE-NAME = Expression

Where "Expression" can be arithmetic or logical.

An example would be

    TOTAL-PRICE = UNIT-PRICE * QUANTITY.

AT the beginning we claimed that COBOL is an English like programming language; therefore COBOL lets the same task to be performed with an assignment statement of the for :

    MOVE UNIT-PRICE * QUANTITY TO TOTAL-PRICE.

or

    COMPUTE TOTAL-PRICE = UNIT-PRICE * QUANTITY.

or

    MULTIPLY UNIT-PRICE BY QUANTITY GIVING TOTAL-PRICE.

Other simple arithmetic expressions can be evaluated within assignment statements as in the following examples. As a programmer, you can use any form you like. Good programming requires that you use the forms consistently. That is, a good COBOL code should use similar statements for similar tasks.

Examples :

    MOVE 1 TO COUNTER.
    MOVE 0 TO INDEX-COUNTER.
    MOVE ZERO TO INDEX-COUNTER.
    MOVE ZERO TO BOOK-COUNTER, AUTHOR-COUNTER, TOTAL-VALUE.

    ADD NEW-VALUE  TO  TOTAL-VALUE.
    ADD NEW-VALUE  TO  OLD-VALUE GIVING TOTAL-VALUE.
    ADD NEW-VALUE, OLD-VALUE GIVING TOTAL-VALUE.
    ADD NEW-VALUE, 100 TO OLD-VALUE.

    SUBTRACT 100 FROM TOTALS.
    SUBTRACT VALUE-READ FROM TOTALS GIVING SUB-TOTAL.
    SUBTRACT VALUE-READ, PREVIOUS-VALUE FROM TOTAL-VALUE.
    SUBTRACT VALUE-READ, PREVIOUS-VALUE FROM TOTAL-VALUE GIVING NEW-TOTAL.

    DIVIDE SMALL-VALUE INTO LARGE-VALUE GIVING DIVISION-RESULT.
    DIVIDE LARGE-VALUE BY SMALL-VALUE GIVING  DIVISION-RESULT.
    COMPUTE DIVISION-RESULT = LARGE-VALUE / SMALL-VALUE.
Please note the order of variables in "DIVIDE ... INTO" and "DIVIDE ... BY" forms! The three division statements above are syntactically different but semantically IDENTICAL to each other.

Like all programming languages, COBOL also permits usage of parentheses to build complex expressions where the order of operators matter.

The precedence order for arithmetic operators is as follows :

Highest Priority+ or - (Unary)
** (Exponentiation)
* and /
Lowest Priority+ and -

The following arithmetic expressions yield DIFFERENT results :

    COMPUTE NEW-AMOUNT = INTEREST-RATE * PRINCIPAL + PREVIOUS-INTEREST.
    COMPUTE NEW-AMOUNT = INTEREST-RATE * (PRINCIPAL + PREVIOUS-INTEREST).

SIMPLEST OUTPUT STATEMENT - DISPLAY

Input/output in COBOL is not VERY simple. Actually it is quite simple and straightforward, but if you want good-looking output you have to use the keyboard extensively to key-in the necessary COBOL code. If you don't care for the looks of your output, you can use the simple and handy DISPLAY command.

If you want to DISPLAY a short mesage on the screen, you can use

    DISPLAY "Hit ENTER key to continue..."
If you want to DISPLAY the value of a variable (either numeric or character string, you can use

    MOVE "Hit ENTER key to continue..." TO MESSAGE-LINE.
    .... Some code ...
    DISPLAY MESSAGE-LINE.
If you need to display multiple items

    DISPLAY "An error occurred.. The code is :",ERR_CODE.
If you are using RM-COBOL you can use some more features with the DISPLAY command :

    DISPLAY MESSAGE-LINE , LINE 23, POSITION 2, ERASE EOL, BLINK, REVERSE, BEEP.
The above command will display the error message on the 23rd line of the screen; starting at column position 23.
Before the message is displayed, the line will be erased staring from 2nd column upto the end of line (ERASE EOL - Erase to End Of Line).
Further, the message will be blinking and will be displayed in reverse video (that is dark text on bright background).
If the terminal or workstation has a beeping facility, the message will be displayed with a BEEP sound to notify the user.

You do not have to specify all these attributes; just choose the ones you like; BUT please do not forget that these are not standard COBOL features. Most COBOL implementations have similar options in the DISPLAY statement but the syntax may vary depending on the manufacturer of the compiler.

SIMPLEST INPUT STATEMENT - ACCEPT

In the previous section's first example, we displayed the message "Hit ENTER key to continue...". Now, how would you know that the user hit the ENTER key? The answer is with the ACCEPT command, the simplest form of which is :

    ACCEPT INPUT-VALUE.


This simple ACCEPT command will read whatever user types (till he/she pressed the ENTER key) at the current screen cursor location. It is the programmer's responsibility that the user enters a reasonable value. If the variable INPUT-VALUE is declared to be numerical; but the user enters his/her name to this ACCEPT statement; an ERROR will occur. We shall study declaring variables and error handling later...

A more sohisticated ACCEPT statement might look like :

    ACCEPT NAME-SURNAME , LINE 14, POSITION 30, PROMPT ".".
This input statement will first take the cursor to (14, 30) and then display some dots (depends on the declared length of the variable NAME-SURNAME). The good point with PROMPT is that, the user will an idea on how long an input he/she can key in.

If you like underscore characters to appear in place of dots, simply do not specify any PROMPT character; or better still type "_" in place of "." in the PROMPT clause.

If the logic of your program requires that the user should be informed about the present value of the variable NAME-SURNAME; you can use

    ACCEPT NAME-SURNAME , LINE 14, POSITION 30, UPDATE, PROMPT ".".
The UPDATE clause instructs the ACCEPT statement to display the current value of the variable first and expect the user to update it. If the user simply hits the ENTER key; the value of the input variable remains UNCHANGED.

Exercise : Implement ACCEPT .... UPDATE, PROMPT "." in C or C++ language... :)


Back to first page...