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:
- What are Variables?
- Defining Variables
- Using Variables in CL Programs
- Practical Examples
- 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
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:
&Nameis a character variable with a length of 20.&Ageis an integer variable.&Salaryis a decimal variable with a total length of 7 digits, including 2 decimal places.&IsEmployedis 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
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:
&Nameis assigned the value 'John Doe'.&Ageis assigned the value 30.&Salaryis assigned the value 55000.75.&IsEmployedis 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)
ENDPGMIn this program:
- A character variable
&Messageis declared with a length of 50. - The variable
&Messageis assigned the value 'Hello, World!'. - The message stored in
&Messageis sent using theSNDPGMMSGcommand.
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)
ENDPGMIn this program:
- Two integer variables
&Num1and&Num2are declared. - An integer variable
&Sumis declared to store the result. &Num1is assigned the value 10.&Num2is assigned the value 20.&Sumis assigned the result of&Num1 + &Num2.- The sum is sent as a message using the
SNDPGMMSGcommand.
Exercises
Exercise 1: Define and Use Variables
- Define a character variable
&FirstNamewith a length of 15. - Define a character variable
&LastNamewith a length of 15. - Define an integer variable
&YearOfBirth. - Assign the value 'Alice' to
&FirstName. - Assign the value 'Smith' to
&LastName. - Assign the value 1990 to
&YearOfBirth. - 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)
ENDPGMExercise 2: Calculate and Display the Product of Two Numbers
- Define two decimal variables
&Num1and&Num2with lengths 5 and 2. - Define a decimal variable
&Productwith length 10 and 2. - Assign the value 12.34 to
&Num1. - Assign the value 56.78 to
&Num2. - Calculate the product of
&Num1and&Num2and store it in&Product. - 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)
ENDPGMConclusion
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.
CL (Control Language) Course
Module 1: Introduction to CL
- What is Control Language?
- Setting Up Your Environment
- Basic Syntax and Structure
- Writing Your First CL Program
Module 2: Basic CL Commands
- Introduction to CL Commands
- File Management Commands
- Job Management Commands
- System Management Commands
Module 3: Variables and Expressions
Module 4: Control Structures
Module 5: Advanced CL Commands
- Advanced File Operations
- Advanced Job Scheduling
- System Configuration Commands
- Security and Permissions
