In this section, we will cover the fundamental syntax and structure of REXX programs. Understanding these basics is crucial for writing and reading REXX code effectively.

Key Concepts

  1. Program Structure
  2. Comments
  3. Statements and Instructions
  4. Whitespace and Line Continuation
  5. Case Sensitivity

  1. Program Structure

A REXX program is a sequence of instructions that the interpreter executes. Here is a simple example of a REXX program:

/* This is a simple REXX program */
say "Hello, World!"
  • Comments: Comments in REXX are enclosed within /* and */. They can span multiple lines.
  • Instructions: Each instruction is typically written on a new line.

  1. Comments

Comments are used to explain the code and are ignored by the interpreter. They can be single-line or multi-line.

/* This is a multi-line comment
   It can span multiple lines */

/* This is a single-line comment */

  1. Statements and Instructions

REXX instructions are the commands that the interpreter executes. Each instruction should be on a new line. Here are some basic instructions:

  • SAY: Outputs text to the screen.
  • PULL: Reads input from the user.

Example:

say "Enter your name:"
pull name
say "Hello, " name

  1. Whitespace and Line Continuation

REXX is not sensitive to whitespace, meaning spaces and tabs are generally ignored. However, for readability, it is good practice to use consistent indentation.

If an instruction is too long to fit on one line, you can continue it on the next line using a comma , at the end of the line.

Example:

say "This is a very long line that we want to split," ,
    "so we use a comma to continue it on the next line."

  1. Case Sensitivity

REXX is case-insensitive for keywords and instructions, but it is case-sensitive for string literals.

Example:

say "Hello, World!"  /* This will work */
SAY "Hello, World!"  /* This will also work */
say "HELLO, WORLD!"  /* This will print in uppercase */

Practical Example

Let's put these concepts together in a simple REXX program:

/* A simple REXX program to demonstrate basic syntax and structure */

say "What is your favorite programming language?"
pull language

if language = "REXX" then
    say "Great choice! REXX is powerful and easy to learn."
else
    say "Interesting! " language " is also a good language."

Explanation

  1. Comments: The program starts with a comment explaining its purpose.
  2. SAY Instruction: Prompts the user to enter their favorite programming language.
  3. PULL Instruction: Reads the user's input into the variable language.
  4. IF/THEN/ELSE Structure: Checks if the input is "REXX" and responds accordingly.

Exercises

Exercise 1

Write a REXX program that asks the user for their age and then prints a message based on the input.

Solution:

/* Program to ask for the user's age and print a message */

say "Please enter your age:"
pull age

if age < 18 then
    say "You are a minor."
else
    say "You are an adult."

Exercise 2

Modify the above program to handle invalid input (non-numeric values).

Solution:

/* Program to ask for the user's age and handle invalid input */

say "Please enter your age:"
pull age

if datatype(age, 'N') then
    if age < 18 then
        say "You are a minor."
    else
        say "You are an adult."
else
    say "Invalid input. Please enter a numeric value."

Common Mistakes and Tips

  • Forgetting to close comments: Ensure every /* has a corresponding */.
  • Misusing case sensitivity: Remember that string comparisons are case-sensitive.
  • Ignoring whitespace: While REXX ignores extra spaces, consistent indentation improves readability.

Conclusion

In this section, we covered the basic syntax and structure of REXX programs, including comments, statements, whitespace handling, and case sensitivity. These fundamentals are essential for writing clear and effective REXX code. In the next section, we will delve into variables and data types, building on the foundation we've established here.

© Copyright 2024. All rights reserved