Hello everyone! This article will guide you on how to declare and define variables in Common Business-Oriented Language (COBOL), while also explaining the logic and meaning behind each keyword used. Variable declaration in COBOL may differ from other programming languages, as it includes some special setter conditions such as initial values, conditions, or constraints in defining how the variables behave , so it’s important to understand why certain keywords exist and how they shape the structure of the program.
Take a look at this code snippet as an example , I will explain little by little!
Code Snippet Examples:
You may find some unusual keywords that you may not find in other compiled languages like C++ or Java for instance, but once you understand it, it will be simple.
Let's go through the new division headers that we haven't covered from our previous article! If you found some of these syntax unfamiliar , do check out my previous ones where I covered about the fundamental components in COBOL here.
Without further ado , let's go!
1. DATA DIVISION
DATA DIVISION.
*> defines the variables that are used in the program for data processing
*> this section is where all data that will be used in the program is defined
DATA DIVISION is used to define all the data items required by the program. It specifies the variables, constants, and file structures that the program will use during execution. In other words, this division acts as the program’s data dictionary, where every piece of data to be processed must first be declared.
2. WORKING-STORAGE SECTION
WORKING-STORAGE SECTION.
*> describes data records that is new and unknown to the file
*> describe data types whose values are assigned in the source program and do not change the execution of the object program
The WORKING-STORAGE SECTION is part of the DATA DIVISION where programmers declare variables and data items that exist for the entire duration of the program. These data items are created when the program starts and remain available until it ends. They are mainly used to hold temporary values, counters, flags, and constants.
3. 01 userName PIC X(20) VALUE "John Doe".
01 userName PIC X(20) VALUE "John Doe".
*> declare a variable named "userName"
*> assign it a value named "John Doe" by putting VALUE keyword before the value
Let's break this into lists of orders (because it's gonna be quite long haha :D)
01 is a level number. It indicates that userName variable is a top-level variable. Whenever you want to declare a new variable , you have to add 01 before naming your variable.
userName is the variable name. You can name it however you want!
PIC is a picture , it tells COBOL ecosystem what kind of data that can be stored in this variable.
X(n) is a condition that enables the variable to accept letters, numbers , symbols as a value. Using X is preferable as it is more flexible!
(20) means the maximum length is 20 characters.
VALUE "John Doe" assigns a default initial value to the variable at program start. In this case, userName will be filled with "John Doe" followed by spaces until the length is 20.
4. 01 userAge PIC 9(3) VALUE 30.
01 userAge PIC 9(3) VALUE 30.
*> declare a variable names "userAge"
*> assign a value 30
In this code , we are now declaring and defining a numeric variable!
Explanation:
PIC 9(n) defines a numeric field of length n digit. Each position can store one digit (0–9).
(3) means the variable can store up to 3 digit (000 - 999).
VALUE 30 initializes the variable with the value 30. Since the field length is 3, COBOL automatically stores it as 030 (leading zero added). If we set the variable value to 100 , COBOL will store it as 100 (no 0 at the front as 100 fulfills the 3 digit length!)
5. 01 userAddress PIC X(60) VALUE "United States".
01 userAddress PIC X(60) VALUE "United States".
*> declare a variable names "userAddress"
*> assign a value "United States"
We apply the same steps that we did for userName variable! But just for a change:
(60) is the new length for the value of this userAddress variable. We sets the maximum length of the variable to 60 characters.
VALUE "United States" , this initializes the variable with the string United States. COBOL will pad the remaining unused positions with spaces until the total length is 60.
6. DISPLAY ourVariable
PROCEDURE DIVISION.
DISPLAY "below is your full information:".
DISPLAY userName.
DISPLAY userAge.
DISPLAY userAddress.
*> DISPLAY the variable that we have declared and defined in WORKING-STORAGE SECTION.
EXIT PROGRAM.
In the PROCEDURE DIVISION, we use the DISPLAY statement to output the values of the variables defined in the WORKING-STORAGE SECTION.
Below are the results from our snippet!
Expected Output:
And there you have it! The output above are the values that we defined from the variables. Therefore , that is how we declare, define and display variables and its values in COBOL.
Thank you for your time. If there is any suggestions, feedbacks, recommendations, do let me know and share in the comments below! See you soon!
Top comments (0)