Variables in COBOL

22 Sep, 2021
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.
While in popular languages, we usually declare and initialize a variable prior to its use in the code, this process is more formal in COBOL, as it has to be declared in a specific area of the program.
Given that a COBOL program is divided into four divisions, we have to look for the Working Storage Section which falls under the Data Division. This is where you will add COBOL data items – what we commonly refer to as variables – in the program.
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. DISPLAY-RANDOM-NUMBER.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    01 NumberOne PIC 9 VALUE 2.
Speaking of which, COBOL works with data such as literals and figurative constants too and which is something that we will look at in the following sections.
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. DISPLAY-RANDOM-NUMBER.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    01 NumberOne PIC 9 VALUE 2.
Very simply, it is composed of parts that consist of a unique level (01) and name (NumberOne), a data category (PIC 9), and an optional value (VALUE 2).
Let’s examine each of these parts in a COBOL declaration in the next section.
Even if the syntax that COBOL adopts seems to be difficult, it’s really very simple. One must remember that this language was developed at a different time, so it might seem different.
So, let’s break down each of the parts as simple as possible, beginning with the data item level number 01:
Cobol
01 NumberOne PIC 9 VALUE 2.
Data items are represented by level numbers and which can be used to group lower-level items under higher-level items, as shown below:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. DISPLAY-STUDENT-INFO.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    01 StudentInfo.        03 MyName PIC A(30) VALUE "My Name".        03 MyStudentID PIC X(8) VALUE "FN107001".        03 MyCGPA PIC 9(2)V9(2) VALUE 3.54.
Clearly, data items with level number 03 can be grouped under the data item level number 01 labeled StudentInfo.
While you can choose numbers between 1-49 to give your data items a sense of hierarchy, it must be pointed out that level number 01 is the topmost. This is what we will use when declaring each data item in a COBOL program.
Cobol
01 NumberOne PIC 9 VALUE 2.
What we now identify with as a variable name is what is referred to as a data name in COBOL. As with naming variables in popular languages, here are a few rules to keep in mind when it comes to COBOL data names:
A data-name can use alphabets a-z and A-Z as well as numbers from 0-9. A hyphen is also permitted.The greatest number of characters that a data name can take is 30 characters.Each data name must have at least one alphabetHyphens cannot be used to begin or end a data name.A data name cannot be a “reserved word”. These keywords that perform certain functions in COBOL. Examples of reserved words include MULTIPLY, COUNT, FOR, ELSE, DISPLAY and ACCESS.
A data-name can use alphabets a-z and A-Z as well as numbers from 0-9. A hyphen is also permitted.
The greatest number of characters that a data name can take is 30 characters.
Each data name must have at least one alphabet
Hyphens cannot be used to begin or end a data name.
A data name cannot be a “reserved word”. These keywords that perform certain functions in COBOL. Examples of reserved words include MULTIPLY, COUNT, FOR, ELSE, DISPLAY and ACCESS.
As you keep these rules in mind, just remember that the data name you select should describe what that variable contains or will be used for.
Cobol
01 NumberOne PIC 9 VALUE 2.
Next, we look at the Picture clause (PIC 9), and which defines the data type and size of the item. Simply put, the system gets a “picture” of this declaration. It serves as a template or an example. Looking at the clause above, the symbol 9 informs the compiler that it should accept single-digit integers only.
Now, what if you want the system to accept double-digit numbers? Here’s the declaration for just that:
Cobol
01 NumberTwo PIC 99 VALUE 22.01 NumberThree PIC 9(2) VALUE 22.
Both these declarations are equally acceptable since they will store the value 22 for both the NumberTwo and NumberThree data names. Similarly, we use the “S” and “V” symbols to indicate sign and decimal places, as shown below:
Cobol
01 NumberFour PIC S9(2) VALUE -32.01 NumberFive PIC 9(2)V9(2) VALUE 21.65.
Lastly, we use the VALUE keyword to assign a value to the data name, as shown below:
Cobol
01 NumberOne PIC 9 VALUE 2.
Of course, there are other types of data that can be declared other than just the numeric type such as the Alphabetic and Alphanumeric types. Using an earlier example, here are the others:
Cobol
03 MyName PIC A(30) VALUE "My Name".03 MyStudentID PIC X(8) VALUE "FN107001".03 MyCGPA PIC 9(2)V9(2) VALUE 3.54.
As we can see above, PIC A is used to declare alphabetic data items while PIC X is used to declare alphanumeric data items. As usual, the value enclosed within each parenthesis indicates the number of digits or characters that need to be allotted for this data item.
Take some time to compare the values assigned to the data names and you’ll understand how the picture clause is used for all three types of data items.
With that said, when declaring and initializing data items in COBOL, please do not forget the full stops after each declaration.
By definition, a literal in COBOL is merely the value that we would associate with the data item. This is the simplest category of data that is used. Based on the example used before and as shown below, the value 2 assigned to NumberOne is literal.
Cobol
01 NumberOne PIC 9 VALUE 2.
Now, there are two types of literals in COBOL: numeric and alphanumeric literals.
As you guessed, numeric literals are just numbers, much like the value shown above. Other examples of numeric literals include -3 and 34.21. In other words, numeric literals also accommodate both signs and decimals.
On the other hand, alphanumeric literals are values that contain both alphabets and numbers or either. Alphanumeric literals are usually enclosed by quotes. Examples of alphanumeric literals include “-321”, “3.24”, “34”, “ans425”, and “answer”.
One thing that must be remembered about literals: you do not have to declare them in the Data Division of a COBOL program but add them directly to the Procedure Division. This is the reason why the VALUE is considered to be optional when declaring data items.
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.
Blogathon-2021
COBOL-Basics
Picked
Blogathon
COBOL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Import JSON Data into SQL Server?
SQL Query to Convert Datetime to Date
Python program to convert XML to Dictionary
Scrape LinkedIn Using Selenium And Beautiful Soup in Python
Shared ViewModel in Android
Difference between COMP and COMP3
Difference Between Sequential, Indexed, and Relative Files in COBOL
Difference Between Search and Search All in COBOL
Loop Statements in COBOL
How to Install COBOL on MacOS?