In this module, we will delve into the core concepts of expressions and operators in Control Language (CL). Understanding these concepts is crucial for writing effective and efficient CL programs. We will cover the following topics:

  1. What are Expressions?
  2. Types of Operators
  3. Arithmetic Operators
  4. Comparison Operators
  5. Logical Operators
  6. Practical Examples
  7. Exercises

  1. What are Expressions?

Expressions in CL are combinations of variables, constants, and operators that are evaluated to produce a value. They are used to perform calculations, make decisions, and manipulate data.

Example:

DCL VAR(&RESULT) TYPE(*DEC) LEN(5 2)
DCL VAR(&NUM1) TYPE(*DEC) LEN(5 2) VALUE(10.50)
DCL VAR(&NUM2) TYPE(*DEC) LEN(5 2) VALUE(2.25)

CHGVAR VAR(&RESULT) VALUE(&NUM1 + &NUM2)

In this example, &NUM1 + &NUM2 is an expression that adds two variables.

  1. Types of Operators

Operators are special symbols that perform operations on variables and values. CL supports several types of operators:

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators

  1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

Operator Description Example
+ Addition &A + &B
- Subtraction &A - &B
* Multiplication &A * &B
/ Division &A / &B

Example:

DCL VAR(&SUM) TYPE(*DEC) LEN(5 2)
DCL VAR(&A) TYPE(*DEC) LEN(5 2) VALUE(5.00)
DCL VAR(&B) TYPE(*DEC) LEN(5 2) VALUE(3.00)

CHGVAR VAR(&SUM) VALUE(&A + &B)

In this example, &A + &B adds the values of &A and &B and stores the result in &SUM.

  1. Comparison Operators

Comparison operators are used to compare two values. The result of a comparison is a Boolean value (*TRUE or *FALSE).

Operator Description Example
= Equal to &A = &B
<> Not equal to &A <> &B
> Greater than &A > &B
< Less than &A < &B
>= Greater or equal &A >= &B
<= Less or equal &A <= &B

Example:

DCL VAR(&IS_EQUAL) TYPE(*LGL)
DCL VAR(&X) TYPE(*DEC) LEN(5 2) VALUE(10.00)
DCL VAR(&Y) TYPE(*DEC) LEN(5 2) VALUE(10.00)

CHGVAR VAR(&IS_EQUAL) VALUE(&X = &Y)

In this example, &X = &Y checks if &X is equal to &Y and stores the result in &IS_EQUAL.

  1. Logical Operators

Logical operators are used to combine multiple conditions. They are often used in conditional statements.

Operator Description Example
& Logical AND &A & &B
` ` Logical OR
! Logical NOT !&A

Example:

DCL VAR(&IS_VALID) TYPE(*LGL)
DCL VAR(&AGE) TYPE(*DEC) LEN(3 0) VALUE(25)
DCL VAR(&IS_MEMBER) TYPE(*LGL) VALUE('*TRUE')

CHGVAR VAR(&IS_VALID) VALUE((&AGE >= 18) & &IS_MEMBER)

In this example, (&AGE >= 18) & &IS_MEMBER checks if &AGE is greater than or equal to 18 and if &IS_MEMBER is true, and stores the result in &IS_VALID.

  1. Practical Examples

Example 1: Calculating Total Price

DCL VAR(&PRICE) TYPE(*DEC) LEN(7 2) VALUE(19.99)
DCL VAR(&QUANTITY) TYPE(*DEC) LEN(3 0) VALUE(3)
DCL VAR(&TOTAL) TYPE(*DEC) LEN(7 2)

CHGVAR VAR(&TOTAL) VALUE(&PRICE * &QUANTITY)

This example calculates the total price by multiplying the price per item by the quantity.

Example 2: Checking Eligibility

DCL VAR(&AGE) TYPE(*DEC) LEN(3 0) VALUE(20)
DCL VAR(&HAS_PERMISSION) TYPE(*LGL) VALUE('*TRUE')
DCL VAR(&IS_ELIGIBLE) TYPE(*LGL)

CHGVAR VAR(&IS_ELIGIBLE) VALUE((&AGE >= 18) & &HAS_PERMISSION)

This example checks if a person is eligible based on their age and permission status.

  1. Exercises

Exercise 1: Basic Arithmetic

Write a CL program that declares two variables, &NUM1 and &NUM2, with values 15 and 5 respectively. Calculate their sum, difference, product, and quotient, and store the results in separate variables.

Solution:

DCL VAR(&NUM1) TYPE(*DEC) LEN(5 2) VALUE(15.00)
DCL VAR(&NUM2) TYPE(*DEC) LEN(5 2) VALUE(5.00)
DCL VAR(&SUM) TYPE(*DEC) LEN(5 2)
DCL VAR(&DIFF) TYPE(*DEC) LEN(5 2)
DCL VAR(&PROD) TYPE(*DEC) LEN(5 2)
DCL VAR(&QUOT) TYPE(*DEC) LEN(5 2)

CHGVAR VAR(&SUM) VALUE(&NUM1 + &NUM2)
CHGVAR VAR(&DIFF) VALUE(&NUM1 - &NUM2)
CHGVAR VAR(&PROD) VALUE(&NUM1 * &NUM2)
CHGVAR VAR(&QUOT) VALUE(&NUM1 / &NUM2)

Exercise 2: Logical Comparison

Write a CL program that declares a variable &SCORE with a value of 85. Check if the score is greater than or equal to 50 and less than or equal to 100. Store the result in a variable &IS_PASS.

Solution:

DCL VAR(&SCORE) TYPE(*DEC) LEN(3 0) VALUE(85)
DCL VAR(&IS_PASS) TYPE(*LGL)

CHGVAR VAR(&IS_PASS) VALUE((&SCORE >= 50) & (&SCORE <= 100))

Conclusion

In this module, we covered the basics of expressions and operators in CL. We learned about different types of operators, including arithmetic, comparison, and logical operators, and how to use them in practical examples. By practicing the exercises, you should now have a solid understanding of how to work with expressions and operators in CL. In the next module, we will explore string manipulation techniques.

© Copyright 2024. All rights reserved