In this section, we will explore the various data types available in COBOL, their usage, and how to declare them. Understanding data types is fundamental to writing effective COBOL programs, as they define the kind of data that can be stored and manipulated within the program.
Key Concepts
- Data Types Overview
 - Numeric Data Types
 - Alphanumeric Data Types
 - Alphabetic Data Types
 - Special Data Types
 - Usage Clauses
 
- Data Types Overview
 
COBOL supports several data types, each designed to handle specific kinds of data. The primary data types in COBOL are:
- Numeric: For storing numbers.
 - Alphanumeric: For storing text and numbers.
 - Alphabetic: For storing letters only.
 - Special: For specific purposes like binary data.
 
- Numeric Data Types
 
Numeric data types in COBOL are used to store numbers, which can be either integers or decimals. They are defined using the PIC (Picture) clause.
Examples:
- Integer: 
PIC 9(5)- A 5-digit integer. - Decimal: 
PIC 9(3)V9(2)- A number with 3 digits before the decimal point and 2 digits after. 
Code Example:
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-INTEGER-VAR     PIC 9(5).
       01  WS-DECIMAL-VAR     PIC 9(3)V9(2).
       PROCEDURE DIVISION.
           MOVE 12345 TO WS-INTEGER-VAR.
           MOVE 123.45 TO WS-DECIMAL-VAR.
           DISPLAY 'Integer Value: ' WS-INTEGER-VAR.
           DISPLAY 'Decimal Value: ' WS-DECIMAL-VAR.
           STOP RUN.Explanation:
WS-INTEGER-VARis declared as a 5-digit integer.WS-DECIMAL-VARis declared as a number with 3 digits before the decimal point and 2 digits after.- The 
MOVEstatements assign values to these variables. - The 
DISPLAYstatements output the values. 
- Alphanumeric Data Types
 
Alphanumeric data types are used to store text and numbers. They are defined using the PIC X clause.
Examples:
- Fixed Length: 
PIC X(10)- A string of 10 characters. - Variable Length: 
PIC X(n)- A string ofncharacters. 
Code Example:
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-ALPHA-VAR       PIC X(10).
       PROCEDURE DIVISION.
           MOVE 'HELLO' TO WS-ALPHA-VAR.
           DISPLAY 'Alphanumeric Value: ' WS-ALPHA-VAR.
           STOP RUN.Explanation:
WS-ALPHA-VARis declared as a 10-character string.- The 
MOVEstatement assigns the string 'HELLO' toWS-ALPHA-VAR. - The 
DISPLAYstatement outputs the value. 
- Alphabetic Data Types
 
Alphabetic data types are used to store letters only. They are defined using the PIC A clause.
Examples:
- Fixed Length: 
PIC A(5)- A string of 5 alphabetic characters. 
Code Example:
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-ALPHABETIC-VAR  PIC A(5).
       PROCEDURE DIVISION.
           MOVE 'ABCDE' TO WS-ALPHABETIC-VAR.
           DISPLAY 'Alphabetic Value: ' WS-ALPHABETIC-VAR.
           STOP RUN.Explanation:
WS-ALPHABETIC-VARis declared as a 5-character alphabetic string.- The 
MOVEstatement assigns the string 'ABCDE' toWS-ALPHABETIC-VAR. - The 
DISPLAYstatement outputs the value. 
- Special Data Types
 
Special data types include:
- COMP-1: Single-precision floating-point.
 - COMP-2: Double-precision floating-point.
 - COMP-3: Packed decimal.
 
Code Example:
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-COMP-3-VAR      PIC 9(5) COMP-3.
       PROCEDURE DIVISION.
           MOVE 12345 TO WS-COMP-3-VAR.
           DISPLAY 'Packed Decimal Value: ' WS-COMP-3-VAR.
           STOP RUN.Explanation:
WS-COMP-3-VARis declared as a 5-digit packed decimal.- The 
MOVEstatement assigns the value 12345 toWS-COMP-3-VAR. - The 
DISPLAYstatement outputs the value. 
- Usage Clauses
 
The USAGE clause specifies how data is stored in memory. Common usage clauses include:
- DISPLAY: Default, human-readable format.
 - COMP: Binary format.
 - COMP-3: Packed decimal format.
 
Code Example:
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-DISPLAY-VAR     PIC 9(5) USAGE DISPLAY.
       01  WS-COMP-VAR        PIC 9(5) USAGE COMP.
       01  WS-COMP-3-VAR      PIC 9(5) USAGE COMP-3.
       PROCEDURE DIVISION.
           MOVE 12345 TO WS-DISPLAY-VAR.
           MOVE 12345 TO WS-COMP-VAR.
           MOVE 12345 TO WS-COMP-3-VAR.
           DISPLAY 'Display Value: ' WS-DISPLAY-VAR.
           DISPLAY 'Binary Value: ' WS-COMP-VAR.
           DISPLAY 'Packed Decimal Value: ' WS-COMP-3-VAR.
           STOP RUN.Explanation:
WS-DISPLAY-VARis stored in a human-readable format.WS-COMP-VARis stored in binary format.WS-COMP-3-VARis stored in packed decimal format.- The 
DISPLAYstatements output the values. 
Practical Exercises
Exercise 1: Declare and Use Numeric Variables
Task: Declare a numeric variable to store a 4-digit integer and a 2-digit decimal. Assign values to these variables and display them.
Solution:
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-INTEGER         PIC 9(4).
       01  WS-DECIMAL         PIC 9(2)V9(2).
       PROCEDURE DIVISION.
           MOVE 1234 TO WS-INTEGER.
           MOVE 12.34 TO WS-DECIMAL.
           DISPLAY 'Integer: ' WS-INTEGER.
           DISPLAY 'Decimal: ' WS-DECIMAL.
           STOP RUN.Exercise 2: Declare and Use Alphanumeric Variables
Task: Declare an alphanumeric variable to store a 15-character string. Assign a value to this variable and display it.
Solution:
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-STRING          PIC X(15).
       PROCEDURE DIVISION.
           MOVE 'COBOL PROGRAM' TO WS-STRING.
           DISPLAY 'String: ' WS-STRING.
           STOP RUN.Common Mistakes and Tips
- Mistake: Forgetting to specify the 
PICclause.- Tip: Always define the 
PICclause to specify the data type and size. 
 - Tip: Always define the 
 - Mistake: Using incorrect data type for the intended data.
- Tip: Choose the appropriate data type based on the kind of data you need to store (numeric, alphanumeric, etc.).
 
 
Conclusion
In this section, we covered the various data types in COBOL, including numeric, alphanumeric, alphabetic, and special data types. We also explored how to declare these data types and use them in COBOL programs. Understanding these data types is crucial for effective data manipulation and storage in COBOL. In the next section, we will delve into variables and constants, building on the foundation laid here.
