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.

  1. 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.

/* Example of variable declaration */
name = "John Doe"
age = 30

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, and NAME 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 and lastName are declared and assigned string values.
  • fullName concatenates firstName and lastName with a space in between using the || operator.
  • say is used to output the value of fullName.

  1. 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 (').

greeting = "Hello, World!"

2.2 Numbers

Numbers in REXX can be integers or floating-point values.

integerNumber = 42
floatingNumber = 3.14

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).

isTrue = 1
isFalse = 0

2.4 Arrays

REXX supports associative arrays (also known as stem variables). These are collections of key-value pairs.

/* Declaring an array */
students.1 = "Alice"
students.2 = "Bob"
students.3 = "Charlie"

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.

  1. 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.

© Copyright 2024. All rights reserved