In this section, we will explore the various operators and expressions used in REXX programming. Understanding these fundamental concepts is crucial for performing calculations, making decisions, and manipulating data in your REXX programs.
Types of Operators
REXX supports several types of operators, including:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Concatenation Operators
- Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations. Here are the basic arithmetic operators in REXX:
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 6 / 3 |
2 |
// |
Integer Division | 7 // 3 |
2 |
** |
Exponentiation | 2 ** 3 |
8 |
% |
Remainder (Modulus) | 7 % 3 |
1 |
Example:
/* Arithmetic Operators Example */ a = 10 b = 3 say "Addition: " a + b say "Subtraction: " a - b say "Multiplication: " a * b say "Division: " a / b say "Integer Division: " a // b say "Exponentiation: " a ** b say "Remainder: " a % b
- Comparison Operators
Comparison operators are used to compare two values. The result of a comparison is either 1
(true) or 0
(false).
Operator | Description | Example | Result |
---|---|---|---|
= |
Equal to | 5 = 5 |
1 |
\= |
Not equal to | 5 \= 3 |
1 |
> |
Greater than | 5 > 3 |
1 |
< |
Less than | 5 < 3 |
0 |
>= |
Greater than or equal to | 5 >= 5 |
1 |
<= |
Less than or equal to | 5 <= 3 |
0 |
Example:
/* Comparison Operators Example */ x = 5 y = 3 say "Equal to: " (x = y) say "Not equal to: " (x \= y) say "Greater than: " (x > y) say "Less than: " (x < y) say "Greater than or equal to: " (x >= y) say "Less than or equal to: " (x <= y)
- Logical Operators
Logical operators are used to combine multiple conditions. The result is either 1
(true) or 0
(false).
Operator | Description | Example | Result |
---|---|---|---|
& |
Logical AND | (5 > 3) & (3 > 1) |
1 |
` | ` | Logical OR | `(5 > 3) |
\ |
Logical NOT | \ (5 > 3) |
0 |
Example:
/* Logical Operators Example */ a = 5 b = 3 c = 1 say "Logical AND: " (a > b) & (b > c) say "Logical OR: " (a > b) | (b < c) say "Logical NOT: " \ (a > b)
- Concatenation Operators
Concatenation operators are used to join strings together.
Operator | Description | Example | Result |
---|---|---|---|
` | ` | Concatenation | |
(space) |
Concatenation with space | "Hello" "World" |
"Hello World" |
Example:
/* Concatenation Operators Example */ str1 = "Hello" str2 = "World" say "Concatenation with ||: " str1 || str2 say "Concatenation with space: " str1 " " str2
Practical Exercises
Exercise 1: Basic Arithmetic
Write a REXX program that takes two numbers as input and performs all arithmetic operations on them.
Solution:
/* Basic Arithmetic Exercise */ say "Enter first number: " pull num1 say "Enter second number: " pull num2 say "Addition: " num1 + num2 say "Subtraction: " num1 - num2 say "Multiplication: " num1 * num2 say "Division: " num1 / num2 say "Integer Division: " num1 // num2 say "Exponentiation: " num1 ** num2 say "Remainder: " num1 % num2
Exercise 2: Comparison and Logical Operations
Write a REXX program that compares two numbers and uses logical operators to combine conditions.
Solution:
/* Comparison and Logical Operations Exercise */ say "Enter first number: " pull num1 say "Enter second number: " pull num2 say "Equal to: " (num1 = num2) say "Not equal to: " (num1 \= num2) say "Greater than: " (num1 > num2) say "Less than: " (num1 < num2) say "Greater than or equal to: " (num1 >= num2) say "Less than or equal to: " (num1 <= num2) say "Logical AND (num1 > num2) & (num2 > 0): " (num1 > num2) & (num2 > 0) say "Logical OR (num1 > num2) | (num2 < 0): " (num1 > num2) | (num2 < 0) say "Logical NOT \ (num1 > num2): " \ (num1 > num2)
Summary
In this section, we covered the various operators and expressions used in REXX programming, including arithmetic, comparison, logical, and concatenation operators. We also provided practical examples and exercises to help reinforce these concepts. Understanding these operators is essential for performing calculations, making decisions, and manipulating data in your REXX programs. In the next section, we will delve into control structures, starting with the IF/THEN/ELSE statements.
REXX Programming Course
Module 1: Introduction to REXX
- What is REXX?
- Setting Up the REXX Environment
- Hello World in REXX
- Basic Syntax and Structure
- Variables and Data Types
Module 2: Basic Programming Concepts
- Operators and Expressions
- Control Structures: IF/THEN/ELSE
- Loops: DO and LEAVE
- Input and Output
- Basic String Manipulation
Module 3: Intermediate REXX Programming
Module 4: Advanced REXX Programming
- Advanced String Manipulation
- Parsing Techniques
- Interfacing with External Programs
- REXX Macros
- Performance Optimization