In this section, we will explore how to declare and use variables and constants in COBOL. Understanding these fundamental concepts is crucial for writing effective COBOL programs.
Key Concepts
- Variables: Storage locations in memory with a name and a data type that can hold different values during the execution of a program.
- Constants: Fixed values that do not change during the execution of a program.
Declaring Variables
In COBOL, variables are declared in the DATA DIVISION. The WORKING-STORAGE SECTION is commonly used for declaring variables that are used throughout the program.
Syntax
01is the level number, indicating the hierarchy of data items.variable-nameis the name of the variable.PIC(Picture) clause specifies the data type and size of the variable.data-typedefines the type of data the variable can hold (e.g., numeric, alphanumeric).
Example
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUMBER PIC 9(5). 01 WS-NAME PIC A(20). 01 WS-AGE PIC 99.
WS-NUMBERis a numeric variable that can hold up to 5 digits.WS-NAMEis an alphanumeric variable that can hold up to 20 characters.WS-AGEis a numeric variable that can hold up to 2 digits.
Declaring Constants
Constants in COBOL are declared using the VALUE clause. They are typically declared in the WORKING-STORAGE SECTION or FILE SECTION.
Syntax
constant-nameis the name of the constant.literalis the fixed value assigned to the constant.
Example
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-MAX-SCORE PIC 9(3) VALUE 100. 01 WS-GREETING PIC X(20) VALUE 'Hello, World!'.
WS-MAX-SCOREis a numeric constant with a value of 100.WS-GREETINGis an alphanumeric constant with a value of 'Hello, World!'.
Practical Example
Let's create a simple COBOL program that demonstrates the use of variables and constants.
Code
IDENTIFICATION DIVISION.
PROGRAM-ID. VariablesExample.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(3).
01 WS-NUM2 PIC 9(3).
01 WS-SUM PIC 9(4).
01 WS-MESSAGE PIC X(30) VALUE 'The sum of the numbers is: '.
PROCEDURE DIVISION.
DISPLAY 'Enter first number: '.
ACCEPT WS-NUM1.
DISPLAY 'Enter second number: '.
ACCEPT WS-NUM2.
ADD WS-NUM1 TO WS-NUM2 GIVING WS-SUM.
DISPLAY WS-MESSAGE WS-SUM.
STOP RUN.Explanation
- IDENTIFICATION DIVISION: Defines the program name.
- DATA DIVISION: Contains the WORKING-STORAGE SECTION where variables and constants are declared.
- PROCEDURE DIVISION: Contains the logic of the program.
DISPLAYstatements prompt the user for input.ACCEPTstatements read the input values intoWS-NUM1andWS-NUM2.ADDstatement addsWS-NUM1andWS-NUM2, storing the result inWS-SUM.- The final
DISPLAYstatement shows the result.
Exercises
Exercise 1
Task: Write a COBOL program that declares two numeric variables and one constant. The program should multiply the two variables and display the result along with a message.
Solution:
IDENTIFICATION DIVISION.
PROGRAM-ID. MultiplyExample.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(3).
01 WS-NUM2 PIC 9(3).
01 WS-PRODUCT PIC 9(6).
01 WS-MESSAGE PIC X(30) VALUE 'The product of the numbers is: '.
PROCEDURE DIVISION.
DISPLAY 'Enter first number: '.
ACCEPT WS-NUM1.
DISPLAY 'Enter second number: '.
ACCEPT WS-NUM2.
MULTIPLY WS-NUM1 BY WS-NUM2 GIVING WS-PRODUCT.
DISPLAY WS-MESSAGE WS-PRODUCT.
STOP RUN.Exercise 2
Task: Modify the previous program to include a constant for the maximum allowed value for the product. If the product exceeds this value, display an error message.
Solution:
IDENTIFICATION DIVISION.
PROGRAM-ID. MultiplyWithLimit.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(3).
01 WS-NUM2 PIC 9(3).
01 WS-PRODUCT PIC 9(6).
01 WS-MESSAGE PIC X(30) VALUE 'The product of the numbers is: '.
01 WS-ERROR PIC X(30) VALUE 'Error: Product exceeds limit.'.
01 WS-MAX-PRODUCT PIC 9(6) VALUE 99999.
PROCEDURE DIVISION.
DISPLAY 'Enter first number: '.
ACCEPT WS-NUM1.
DISPLAY 'Enter second number: '.
ACCEPT WS-NUM2.
MULTIPLY WS-NUM1 BY WS-NUM2 GIVING WS-PRODUCT.
IF WS-PRODUCT > WS-MAX-PRODUCT THEN
DISPLAY WS-ERROR
ELSE
DISPLAY WS-MESSAGE WS-PRODUCT
END-IF.
STOP RUN.Summary
In this section, we covered:
- The declaration and use of variables and constants in COBOL.
- Practical examples demonstrating how to work with variables and constants.
- Exercises to reinforce the concepts learned.
Understanding variables and constants is essential for managing data in your COBOL programs. In the next section, we will delve into arithmetic operations, building on the knowledge gained here.
