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 start working 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.
000700 BEGIN.
000800 DISPLAY “Hello World!”.
000900 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.
000520 01 WS-A PIC 999.
000530 01 WS-B PIC 9 (3).
000540 01 WS-RESULT PIC9 (6).
In PROCEDURE DIVISION. accordingly there will be the following lines:
000700 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.
We traditionally save it to a file with the .cob extension and run the compiler. (We did not forget to add the missing parts of the program, right? ;-))
We launch 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
300 and 100, I entered, the rest was displayed by the program. The results speak for themselves, and now we will consider new lines of code that we have introduced more closely.
Let's start with new products in DATA DIVISION.
000510 WORKING-STORAGE SECTION. - informs the compiler about the beginning of the section describing ordinary variables.
000520 01 WS-A PIC 999.
000530 01 WS-B PIC 9 (3).
000540 01 WS-RESULT PIC 9 (6).
From the point of view of the semantic load, these three lines are identical, they define three variables with level 01, followed by the variable name (WS-A, WS-B and WS-RESULT), then comes 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 not indicated by the number of bits to be allocated, but by the number of maximum characters.
In our case, we have described two variables that will contain 3 SIGNIFICANT numbers and one variable that will contain 6 SIGNIFICANT numbers.
More on the description of the format. 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 record form 9 (15) was made, that is, the number of times is indicated in brackets which repeats the character in front of the brackets.
We turn to PROCEDURE DIVISION.
DISPLAY - as you can understand from the program and its output deals with 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 quotes “”. You can display several variables or texts, as seen in the case of
001700 DISPLAY “Summ is:”, WS-RESULT.
The main thing is not to forget the comma between them.
Also in the program were noted 4 mathematical functions:
Addition ADD WS-A to WS-B GIVING WS-RESULT.
Subtraction SUBTRACT WS-A FROM WS-B GIVING WS-RESULT.
Multiplication MULTIPLY WS-A BY WS-B GIVING WS-RESULT.
Division DIVIDE WS-B BY WS-A GIVING WS-RESULT.
They are built on the same principle.
<operator> value1 TO / FROM / BY value2 GIVING variable.
Values ​​can be either variables or numbers directly, for example,
ADD 10 TO 20 GIVING WS-RESULT.
Add 10 to 20 and record the result (30) in the WS-RESULT.
If GIVING and the subsequent variable name are omitted, the result will be written into the variable going 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 some other forms of recording, 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 also omit TO - ADD 1 WS-A 2 WS-B adds all these values ​​and writes the result to WS-B. Several variables can be specified after TO (values ​​for each of the variables after TO will be added to TO and the result written to the corresponding variable) and after GIVING, then the result will be written to each of them. Similarly, you can operate with SUBTRACT.
BUT in the case of MULTIPLY and DIVIDE, specifying several variables is possible only after GIVING (and this should be clarified for each compiler separately).
In addition, DIVIDE has a second form of recording.
DIVIDE WS-A INTO WS-B GIVING WS-C with a change of BY to INTO also changes the order of variables, which particular form to use remains the choice of the user. I will only indicate that:
DIVIDE WS-A INTO WS-B is WS-B / WS-A = WS-B
DIVIDE WS-B BY WS-A GIVING WS-RESULT
This is WS-B / WS-A = WS-RESULT
IT IS IMPORTANT! The variant with BY WILL NOT work without GIVING. The program is simply not compiled due to an error.
And finally, a small “gift” - surely the reader has already wondered “how can we assign the 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 surely you are surprised by the numbers that we got on the output and the number of zeros in them? This we will consider in the next article.
Vorontsov “nerfur” Vyacheslav. 2011
IMPORTANT UPDATE! Unfortunately forgot to specify one important keyword for DIVIDE.
DIVIDE WS-B BY WS-A GIVING WS-RESULT REMINDER <variable>
Using REMAINDER we specify the variable in which the REMAINST division is written.