COBOL Data Types and Variables

May 3, 2024

In this lesson, we explore the different data types available in COBOL and how variables are declared and used. Understanding data types is crucial for effective programming in COBOL.

Data Types in COBOL

COBOL supports various data types to represent different kinds of information. Some of the common data types in COBOL include:

  • Numeric data types: COMP, COMP-3, DISPLAY
  • Alphanumeric data types: CHARACTER, ALPHANUMERIC
  • Usage-specific data types: PACKED-DECIMAL, INDEX

Variables in COBOL

In COBOL, variables are declared using the DATA DIVISION section of the program. Here’s an example of how variables are declared in COBOL:

01 EMPLOYEE-ID PIC X(5).
01 EMPLOYEE-NAME PIC X(20).
01 EMPLOYEE-AGE PIC 99.
01 EMPLOYEE-SALARY PIC 9(5)V99.

In the above example, we have declared variables to store employee details such as ID, name, age, and salary. The PIC clause is used to specify the data type and size of the variable.

Using Variables in COBOL

Once variables are declared, they can be used in the program to store and manipulate data. Here’s an example of how variables are used in COBOL:

MOVE 'John' TO EMPLOYEE-NAME.
MOVE 30 TO EMPLOYEE-AGE.
ADD 5000.50 TO EMPLOYEE-SALARY.

In the above code snippet, we are assigning values to the variables EMPLOYEE-NAME, EMPLOYEE-AGE, and EMPLOYEE-SALARY using the MOVE statement. We are also performing arithmetic operations on the EMPLOYEE-SALARY variable using the ADD statement.

Understanding data types and variables in COBOL is essential for writing efficient and error-free programs. By mastering these concepts, you will be able to create robust COBOL applications that meet your business requirements.