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

  1. Variables: Storage locations in memory with a name and a data type that can hold different values during the execution of a program.
  2. 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

01 variable-name PIC data-type.
  • 01 is the level number, indicating the hierarchy of data items.
  • variable-name is the name of the variable.
  • PIC (Picture) clause specifies the data type and size of the variable.
  • data-type defines 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-NUMBER is a numeric variable that can hold up to 5 digits.
  • WS-NAME is an alphanumeric variable that can hold up to 20 characters.
  • WS-AGE is 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

01 constant-name PIC data-type VALUE literal.
  • constant-name is the name of the constant.
  • literal is 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-SCORE is a numeric constant with a value of 100.
  • WS-GREETING is 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

  1. IDENTIFICATION DIVISION: Defines the program name.
  2. DATA DIVISION: Contains the WORKING-STORAGE SECTION where variables and constants are declared.
  3. PROCEDURE DIVISION: Contains the logic of the program.
    • DISPLAY statements prompt the user for input.
    • ACCEPT statements read the input values into WS-NUM1 and WS-NUM2.
    • ADD statement adds WS-NUM1 and WS-NUM2, storing the result in WS-SUM.
    • The final DISPLAY statement 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.

© Copyright 2024. All rights reserved