In this section, we will delve into the concept of variables in Control Language (CL). Understanding how to define and use variables is fundamental to writing effective CL programs. We will cover the following topics:

  1. What are Variables?
  2. Defining Variables
  3. Using Variables in CL Programs
  4. Practical Examples
  5. Exercises

What are Variables?

Variables are placeholders used to store data that can be referenced and manipulated within a program. They allow you to write flexible and dynamic code by enabling you to work with data that can change during the execution of the program.

Defining Variables

In CL, variables are defined using the DCL (Declare) command. When defining a variable, you need to specify its name, type, and length (if applicable). Here are the common types of variables in CL:

  • Character (CHAR): Used to store text.
  • Decimal (DEC): Used to store numeric values with decimal points.
  • Integer (INT): Used to store whole numbers.
  • Logical (LGL): Used to store boolean values (true/false).

Syntax for Declaring Variables

DCL VAR(&VariableName) TYPE(*Type) LEN(Length)
  • VAR(&VariableName): The name of the variable, prefixed with an ampersand (&).
  • TYPE(*Type): The type of the variable (e.g., *CHAR, *DEC, *INT, *LGL).
  • LEN(Length): The length of the variable (only applicable for character and decimal types).

Example

DCL VAR(&Name) TYPE(*CHAR) LEN(20)
DCL VAR(&Age) TYPE(*INT)
DCL VAR(&Salary) TYPE(*DEC) LEN(7 2)
DCL VAR(&IsEmployed) TYPE(*LGL)

In this example:

  • &Name is a character variable with a length of 20.
  • &Age is an integer variable.
  • &Salary is a decimal variable with a total length of 7 digits, including 2 decimal places.
  • &IsEmployed is a logical variable.

Using Variables in CL Programs

Once variables are defined, you can use them to store and manipulate data. You can assign values to variables using the CHGVAR (Change Variable) command.

Syntax for Assigning Values

CHGVAR VAR(&VariableName) VALUE(Value)
  • VAR(&VariableName): The name of the variable to which you want to assign a value.
  • VALUE(Value): The value to be assigned to the variable.

Example

DCL VAR(&Name) TYPE(*CHAR) LEN(20)
DCL VAR(&Age) TYPE(*INT)
DCL VAR(&Salary) TYPE(*DEC) LEN(7 2)
DCL VAR(&IsEmployed) TYPE(*LGL)

CHGVAR VAR(&Name) VALUE('John Doe')
CHGVAR VAR(&Age) VALUE(30)
CHGVAR VAR(&Salary) VALUE(55000.75)
CHGVAR VAR(&IsEmployed) VALUE('1')

In this example:

  • &Name is assigned the value 'John Doe'.
  • &Age is assigned the value 30.
  • &Salary is assigned the value 55000.75.
  • &IsEmployed is assigned the value '1' (true).

Practical Examples

Example 1: Simple Variable Assignment

PGM
    DCL VAR(&Message) TYPE(*CHAR) LEN(50)
    CHGVAR VAR(&Message) VALUE('Hello, World!')
    SNDPGMMSG MSG(&Message)
ENDPGM

In this program:

  • A character variable &Message is declared with a length of 50.
  • The variable &Message is assigned the value 'Hello, World!'.
  • The message stored in &Message is sent using the SNDPGMMSG command.

Example 2: Calculating the Sum of Two Numbers

PGM
    DCL VAR(&Num1) TYPE(*INT)
    DCL VAR(&Num2) TYPE(*INT)
    DCL VAR(&Sum) TYPE(*INT)

    CHGVAR VAR(&Num1) VALUE(10)
    CHGVAR VAR(&Num2) VALUE(20)
    CHGVAR VAR(&Sum) VALUE(&Num1 + &Num2)

    SNDPGMMSG MSG('The sum is ' *CAT &Sum)
ENDPGM

In this program:

  • Two integer variables &Num1 and &Num2 are declared.
  • An integer variable &Sum is declared to store the result.
  • &Num1 is assigned the value 10.
  • &Num2 is assigned the value 20.
  • &Sum is assigned the result of &Num1 + &Num2.
  • The sum is sent as a message using the SNDPGMMSG command.

Exercises

Exercise 1: Define and Use Variables

  1. Define a character variable &FirstName with a length of 15.
  2. Define a character variable &LastName with a length of 15.
  3. Define an integer variable &YearOfBirth.
  4. Assign the value 'Alice' to &FirstName.
  5. Assign the value 'Smith' to &LastName.
  6. Assign the value 1990 to &YearOfBirth.
  7. Send a message that combines &FirstName, &LastName, and &YearOfBirth.

Solution

PGM
    DCL VAR(&FirstName) TYPE(*CHAR) LEN(15)
    DCL VAR(&LastName) TYPE(*CHAR) LEN(15)
    DCL VAR(&YearOfBirth) TYPE(*INT)

    CHGVAR VAR(&FirstName) VALUE('Alice')
    CHGVAR VAR(&LastName) VALUE('Smith')
    CHGVAR VAR(&YearOfBirth) VALUE(1990)

    SNDPGMMSG MSG('Name: ' *CAT &FirstName *BCAT &LastName *BCAT 'Year of Birth: ' *CAT &YearOfBirth)
ENDPGM

Exercise 2: Calculate and Display the Product of Two Numbers

  1. Define two decimal variables &Num1 and &Num2 with lengths 5 and 2.
  2. Define a decimal variable &Product with length 10 and 2.
  3. Assign the value 12.34 to &Num1.
  4. Assign the value 56.78 to &Num2.
  5. Calculate the product of &Num1 and &Num2 and store it in &Product.
  6. Send a message displaying the product.

Solution

PGM
    DCL VAR(&Num1) TYPE(*DEC) LEN(5 2)
    DCL VAR(&Num2) TYPE(*DEC) LEN(5 2)
    DCL VAR(&Product) TYPE(*DEC) LEN(10 2)

    CHGVAR VAR(&Num1) VALUE(12.34)
    CHGVAR VAR(&Num2) VALUE(56.78)
    CHGVAR VAR(&Product) VALUE(&Num1 * &Num2)

    SNDPGMMSG MSG('The product is ' *CAT &Product)
ENDPGM

Conclusion

In this section, we have learned how to define and use variables in CL. We covered the syntax for declaring variables, assigning values to them, and using them in practical examples. By practicing the exercises, you should now have a solid understanding of how to work with variables in CL programs. In the next section, we will explore expressions and operators, which will allow you to perform more complex operations with your variables.

© Copyright 2024. All rights reserved