Meet COBOL - Part 2

  • Tutorial
Well, let's continue our acquaintance further, in the first article we learned what the COBOL program consists of, what rules exist for its writing and launched our first program. It's time to move on, this time we will begin to work with variables and perform the first meaningful actions with them.


So, back to our program.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. HELLOWORLD.
000300 * --- This is an empty line. ---
000400 ENVIRONMENT DIVISION.
000500 DATA DIVISION.
000600 PROCEDURE DIVISION.
000,700 BEGIN.
000800 DISPLAY “Hello World!”.
000,900 STOP RUN.


Add to DATA DIVISION. new lines, namely the WORKING-STORAGE SECTION section. and a couple of WS-A and WS-B variables.

It will look like this:

000500 DATA DIVISION.
000510 WORKING-STORAGE SECTION.
000 520 01 WS-A PIC 999.
000 530 01 WS-B PIC 9 (3).
000 540 01 WS-RESULT PIC9 (6).


At PROCEDURE DIVISION. accordingly there will be the following lines:

000,700 BEGIN.
000800 DISPLAY “Hello I'm your new calculator!”.
000900 DISPLAY “Please Enter first number from 0 to 999”.
001000 ACCEPT WS-A.
001100 DISPLAY “Please Enter second number from 0 to 999”.
001200 ACCEPT WS-B.
001300 DISPLAY “------------------------------------".
001400 DISPLAY ““.
001500 DISPLAY “Your results are:”.
001600 ADD WS-A TO WS-B GIVING WS-RESULT.
001700 DISPLAY “Summ is:”, WS-RESULT.
001800 SUBTRACT WS-A FROM WS-B GIVING WS-RESULT.
001900 DISPLAY “Subtract is:”, WS-RESULT.
002000 MULTIPLY WS-A BY WS-B GIVING WS-RESULT.
002100 DISPLAY “Multiplication is:”, WS-RESULT.
002200 DIVIDE WS-A BY WS-B GIVING WS-RESULT.
002300 DISPLAY “Divide is:”, WS-RESULT.
002400 STOP RUN.


Traditionally, we save this to a file with the extension .cob and run the compiler. (We didn’t forget to add the missing parts of the program, right? ;-))

We launch the successfully assembled executable file and see, for example:

Hello I'm your new calculator!
Please Enter first number from 0 to 999
300
Please Enter second number from 0 to 999
100
------------------------------------
Your results are:
Summ is: 000400
Subtract is: 000200
Multiplication is: 030000
Divide is: 000003


I entered 300 and 100, the rest was deduced by the program. The results speak for themselves, and now consider the new lines of code that we have introduced more closely.

Let's start with the new DATA DIVISION.

000510 WORKING-STORAGE SECTION. - informs the compiler about the beginning of the section describing ordinary variables.
000 520 01 WS-A PIC 999.
000 530 01 WS-B PIC 9 (3).
000 540 01 WS-RESULT PIC 9 (6).


From the point of view of semantic load, these three lines are identical, they define three variables with level 01, followed by the name of the variable (WS-A, WS-B and WS-RESULT), then the PIC operator (which can also be written in its older form like PICTURE IS), which sets the format and size of a variable. And here begins the main surprise for those who are familiar with other languages. The size and format is indicated not by the number of bits allocated, but by the number of maximum characters.
In our case, we described two variables that will contain a 3 VALUE number and one variable that will contain a 6 VALUE number.

Learn more about the format description. 9 - tells the compiler that the variable will be numeric, the number of characters indicates the number of digits, it is natural that for large numbers it is inconvenient to write and read something a la 999999999999999 and the form of writing 9 (15) was made, that is, the number of times is indicated in brackets which repeated the character before the brackets.

Let's move on to the PROCEDURE DIVISION.
DISPLAY - as you can understand from the program and its output, it is engaged in one of the most useful functions in the world. Displays the specified variable or text on the screen. Custom text must be enclosed in double quotation marks “”. Several variables or texts can be output, as seen with

001700 DISPLAY “Summ is:”, WS-RESULT.

the main thing is not to forget the comma between them.

The program also noted 4 mathematical functions:
Addition ADD WS-A TO WS-B GIVING WS-RESULT.
Subtract SUBTRACT WS-A FROM WS-B GIVING WS-RESULT.
Multiplication MULTIPLY WS-A BY WS-B GIVING WS-RESULT.
DIVIDE division WS-B BY WS-A GIVING WS-RESULT.

They are built on the same principle.

<operator> value1 TO / FROM / BY value2 GIVING variable.

The values ​​can be either variables or numbers directly, for example,

ADD 10 TO 20 GIVING WS-RESULT.

Add 10 to 20 and write the result (30) in WS-RESULT.

If GIVING and the subsequent variable name are omitted, then the result is written to the variable after AFTER TO, FROM, BY - in our case it will be WS-B (but WS-A in the case of DIVIDE).

A small but important note, there are slightly different forms of writing, for example:

ADD 1 2 3 TO WS-B - respectively, first add 1 + 2 + 3, and then add it to the value of WS-B and write the result to WS-B.
You can omit and TO - ADD 1 WS-A 2 WS-B adds all these values ​​and writes the result to WS-B. Several variables can be specified both after TO (to each of the variables after TO the values ​​will be added before TO and the result is written to the corresponding variable) and after GIVING, then the result will be written in each of them. Similarly, you can operate with SUBTRACT.

BUT in the case of MULTIPLY and DIVIDE, the indication of several variables is possible only after GIVING (and this must be specified for each compiler separately).

In addition, DIVIDE has a second form of recording.
DIVIDE WS-A INTO WS-B GIVING WS-C with the change of BY to INTO also changes the order of the variables, which form to use remains the choice of the user. I will only point out that:
DIVIDE WS-A INTO WS-B is WS-B / WS-A = WS-B

DIVIDE WS-B BY WS-A GIVING WS-RESULT
is WS-B / WS-A = WS-RESULT

THIS IMPORTANT! The BY option will NOT work without GIVING. The program simply does not compile due to an error.

And finally, a small “gift” - probably the reader already wondered “how to assign a value to a variable?”. Using MOVE

MOVE 10 TO WS-A
or MOVE WS-A TO WS-B
or even MOVE 10 TO WS-A WS-B

IT IS IMPORTANT! MOVE accepts several variables only at the “output”, i.e. after TO.

PS And you are probably surprised by the numbers that we got on the output and the number of zeros in them? We will consider this in the next article.


Vorontsov “nerfur” Vyacheslav. 2011.

IMPORTANT UPDATE! Unfortunately, I forgot to indicate one important keyword for DIVIDE.
DIVIDE WS-B BY WS-A GIVING WS-RESULT REMINDER <variable>

Using REMAINDER we specify a variable to which the RESIDUE from division is written.

Also popular now: