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

  1. Data Types Overview
  2. Numeric Data Types
  3. Alphanumeric Data Types
  4. Alphabetic Data Types
  5. Special Data Types
  6. Usage Clauses

  1. 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.

  1. 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-VAR is declared as a 5-digit integer.
  • WS-DECIMAL-VAR is declared as a number with 3 digits before the decimal point and 2 digits after.
  • The MOVE statements assign values to these variables.
  • The DISPLAY statements output the values.

  1. 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 of n characters.

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-VAR is declared as a 10-character string.
  • The MOVE statement assigns the string 'HELLO' to WS-ALPHA-VAR.
  • The DISPLAY statement outputs the value.

  1. 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-VAR is declared as a 5-character alphabetic string.
  • The MOVE statement assigns the string 'ABCDE' to WS-ALPHABETIC-VAR.
  • The DISPLAY statement outputs the value.

  1. 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-VAR is declared as a 5-digit packed decimal.
  • The MOVE statement assigns the value 12345 to WS-COMP-3-VAR.
  • The DISPLAY statement outputs the value.

  1. 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-VAR is stored in a human-readable format.
  • WS-COMP-VAR is stored in binary format.
  • WS-COMP-3-VAR is stored in packed decimal format.
  • The DISPLAY statements 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 PIC clause.
    • Tip: Always define the PIC clause to specify the data type and size.
  • 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.

© Copyright 2024. All rights reserved