In this section, we will explore how to declare and use variables in REXX, as well as the different data types available. Understanding these concepts is fundamental to writing effective REXX programs.
- Variables in REXX
1.1 Declaring Variables
In REXX, variables are declared simply by assigning a value to them. There is no need to specify the data type explicitly.
1.2 Variable Naming Rules
- Variable names must start with a letter (A-Z, a-z) or a special character (such as
@
,#
,$
). - Subsequent characters can be letters, digits (0-9), or special characters.
- Variable names are case-insensitive (
name
,Name
, andNAME
refer to the same variable).
1.3 Example
/* Declaring and using variables */ firstName = "Alice" lastName = "Smith" fullName = firstName || " " || lastName say "Full Name: " fullName
Explanation:
firstName
andlastName
are declared and assigned string values.fullName
concatenatesfirstName
andlastName
with a space in between using the||
operator.say
is used to output the value offullName
.
- Data Types in REXX
REXX is a typeless language, meaning it does not enforce data types. However, it supports various types of data implicitly:
2.1 Strings
Strings are sequences of characters enclosed in double quotes ("
) or single quotes ('
).
2.2 Numbers
Numbers in REXX can be integers or floating-point values.
2.3 Boolean Values
REXX does not have a distinct boolean type. Instead, it uses integers (0
for false, non-zero for true) or strings ("0"
for false, any other string for true).
2.4 Arrays
REXX supports associative arrays (also known as stem variables). These are collections of key-value pairs.
2.5 Example
/* Using different data types */ name = "John Doe" age = 25 height = 5.9 isStudent = 1 say "Name: " name say "Age: " age say "Height: " height say "Is Student: " isStudent
Explanation:
name
is a string.age
is an integer.height
is a floating-point number.isStudent
is used as a boolean value.
- Practical Exercises
Exercise 1: Variable Declaration and Usage
Task: Declare variables for a person's first name, last name, age, and height. Concatenate the first name and last name to form the full name and print all the details.
/* Solution */ firstName = "Jane" lastName = "Doe" age = 28 height = 5.7 fullName = firstName || " " || lastName say "Full Name: " fullName say "Age: " age say "Height: " height
Exercise 2: Using Arrays
Task: Create an array to store the names of three cities and print each city name.
/* Solution */ cities.1 = "New York" cities.2 = "Los Angeles" cities.3 = "Chicago" do i = 1 to 3 say "City " i ": " cities.i end
Common Mistakes and Tips
- Mistake: Forgetting that variable names are case-insensitive.
- Tip: Stick to a consistent naming convention to avoid confusion.
- Mistake: Using uninitialized variables.
- Tip: Always initialize variables before using them.
Conclusion
In this section, we covered the basics of variables and data types in REXX. You learned how to declare variables, the implicit data types supported by REXX, and how to use arrays. These foundational concepts will be crucial as you progress to more complex programming tasks in REXX. Next, we will delve into operators and expressions, which will allow you to perform various operations on the data stored in your variables.
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