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:
In contrast, in Python, which is a loosely typed language, we use the syntax without the data type to do the same:
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.
Introduction to COBOL Variables
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.
Variables in COBOL Syntax
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 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.
Literals in COBOL
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.
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.
Similar Reads
Global Variables in MATLAB
Variables in programming are generally storage spaces to store a certain type of data. There are many types of variables, but two commonly used types are local and Global variables. Generally, each MATLAB function has its own local variables. But sometimes for the sake of programming, we need to cha
4 min read
Python Variables
In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
7 min read
Control Variables in Statistics
Control Variable is a type of variable used to verify the accuracy of any experiment, as the control variable is an essential part of experimental design. Control Variables are used extensively in the field of research where experiments are conducted to compare the new approach to the standard basel
5 min read
Variables in LISP
Similar to other languages, in LISP variables are named places that store a particular value, but they are not declared the way you declare variables in C++ or Java i.e you don't need to mention data-type of variables when declaring them as LISP is dynamically typed. LISP supports two types of varia
3 min read
Perl | Variables
Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, deci
4 min read
Subroutines in COBOL
Subroutines in COBOL are small programs that are compiled independently but cannot be run directly. Cobol subroutine is a small program that can be compiled independently but cannot be run directly. There are two types of COBOL Â subroutines- internal subroutines and the external subroutines Call Ver
3 min read
Search in COBOL
The SEARCH keyword is used to check for the presence or absence of some particular elements in any tables or arrays. We can find an element either by performing loop operations or by using the SEARCH keyword. We can even go for SEARCH ALL but this article will read about the SEARCH keywords only. Sy
2 min read
Variables in Objective-C
A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
5 min read
Variable in Maths
A variable is like a placeholder or a box that can hold different values. In math, it's often represented by a letter, like x or y. The value of a variable can change depending on the situation. For example, if you have the equation y = 2x + 3, the value of y depends on the value of x. So, if you ch
5 min read
Linkage Section in COBOL
The linkage Section in COBOL is used to declare the variables and it is used in calling the variable from another program. It is used in calling the variable or data from the called program by using the CALL statement. The linkage Section helps in sending the data to the calling program or receivin
4 min read