Figurative Constants in COBOL
Now, there are times when using literals to initialize a data item might not be pertinent or necessary. For example, you might just want to fill a data item with an arbitrary sequence of zeroes. Here’s what you can do:
Cobol
01 myvar PIC X( 10 ) VALUE ZEROES. |
Or if a data item needs to be filled with other characters such as quotes, spaces among other ASCII values based on the size of the data item.
This is where figurative constants – being reserved words – might come in handy and which will fill the entire item. A literal assigned by the user will not be able to do this.
Cobol
01 myvar PIC X( 20 ) VALUE SPACES. |
Another reason why figurative constants are good is because of the readability they offer. If you compare the two statements of the data items, which reads better?
Cobol
01 myvar PIC X( 10 ) VALUE ZEROES. 01 myvar2 PIC X( 10 ) VALUE "0000000000" . |
The form used by the first statement seems better than the second, for obvious reasons.
Example: Run a Sample COBOL Program
Now, muscle memory is very important when learning to code. We cannot learn programming just by reading but have to try a sample COBOL program too. Here’s an example of a COBOL program that displays details of a particular employee:
Cobol
IDENTIFICATION DIVISION . PROGRAM - ID . DISPLAY -STUDENT-INFO. ENVIRONMENT DIVISION . DATA DIVISION . WORKING-STORAGE SECTION . 01 eName PIC A( 12 ) VALUE "Rahul Biswas" . 01 Occupation PIC A( 10 ) VALUE "Programmer" . 01 EmployeeID PIC X( 10 ) VALUE "MSFT107001" . 01 Salary PIC 9 ( 2 )V9( 2 ) VALUE 3.23 . PROCEDURE DIVISION . DISPLAY "My Name:" eName DISPLAY "My Occupation: " Occupation DISPLAY "Employee ID: " EmployeeID DISPLAY "My CGPA: " Salary STOP RUN . END PROGRAM DISPLAY -STUDENT-INFO. |
Output:
My Name:Rahul Biswas My Occupation: Programmer Employee ID: MSFT107001 My CGPA: 03.23
One final thing: the environment division, while not necessary in this sample, has been placed just to show you the order of the four divisions of a COBOL program.
Having said that, our introduction to COBOL variables has now concluded. Or as they’re called: data items.
Variables in COBOL
Declaring and initializing variables might be simple for programmers. However, if this is your first programming language, think of a variable as a container in which you can fill an unknown quantity – a value of said data type. Based on its use, this variable’s value is then stored at a particular memory address and which can be accessed based on the variable name chosen by the programmer. Now, when we use a variable, we declare and often initialize it with a value that could be of boolean, string, int, or double type. Most importantly, we use descriptive names so that we know what that variable is being used for.
For example, in Java, which is a strongly typed language, we would use the following syntax:
Java
int loopCounter = 0 ; |
In contrast, in Python, which is a loosely typed language, we use the syntax without the data type to do the same:
Python3
loopCounter = 0 |
In COBOL, this is almost the same. Particularly if we want to declare and initialize variables for computation.
That said, we refer to variables as data items in COBOL and for the sake of following conventionally accepted terms when learning this language, we’ll do the same.