Rexx Programming/Introduction/syntax

Looping
The loop control structure in Rexx begins with a  and ends with an   but comes in several varieties. NetRexx uses the keyword  instead of   for looping, while ooRexx treats   and   as equivalent when looping.

Traditional forms:

With an index variable:

another variant:

The step increment (z) may be omitted and defaults to 1. The upper limit (y) can also be omitted, which makes the loop continue forever. You can also loop forever without an index variable with this:

A program can break out of the current loop with the  instruction (which is the normal way to exit a "forever" loop), or can short-circuit it with the   instruction.

The  and   forms are equivalent to:

and:

Conditionals
Testing conditions with :

The  clause is optional.

Or, to put it more concisely:

For single instructions,  and   can also be omitted:

Or, in another concise version:

Indentation is optional, but it helps improve the readability.

Testing for multiple conditions
is Rexx's CASE structure, like many other constructs derived from PL/I:

indicates no instruction is to be executed.

The OTHERWISE clause is optional. If omitted and no WHEN conditions are met, then the SYNTAX condition is raised.

Simple variables
Variables in Rexx are typeless, and initially are evaluated as their names, in upper case. Thus a variable's type can vary with its use in the program:

Compound variables
Unlike many other programming languages, classic Rexx has no direct support for arrays of variables addressed by a numerical index. Instead it provides compound variables. A compound variable consists of a stem followed by a tail. A. (dot) is used to join the stem to the tail. If the tails used are numeric, it is easy to produce the same effect as an array.

Afterwards the following variables with the following values exist: stem.1 = 9, stem.2 = 8, stem.3 = 7...

Unlike arrays, the index for a stem variable is not required to have an integer value. For example, the following code is valid:

In Rexx it is also possible to set a default value for a stem.

After these assignments the term  would produce.

The whole stem can also be erased with the DROP statement.

This also has the effect of removing any default value set previously.

By convention (and not as part of the language) the compound  is often used to keep track of how many items are in a stem, for example a procedure to add a word to a list might be coded like this:

It is also possible to have multiple elements in the tail of a compound variable. For example:

Multiple numerical tail elements can be used to provide the effect of a multi-dimensional array.

Features similar to Rexx compound variables are found in many other languages (including associative arrays in AWK, hashes in Perl and Hashtables in Java). Most of these languages provide an instruction to iterate over all the keys (or tails in Rexx terms) of such a construct, but this is lacking in classic Rexx. Instead it is necessary to keep auxiliary lists of tail values as appropriate. For example, in a program to count words the following procedure might be used to record each occurrence of a word.

and then later:

At the cost of some clarity it is possible to combine these techniques into a single stem:

and later:

Rexx provides no safety net here, so if one of the words happens to be a whole number less than  this technique will fail mysteriously.

Recent implementations of Rexx, including IBM's Object REXX and the open source implementations like ooRexx include a new language construct to simplify iteration over the value of a stem, or over another collection object such as an array, table or list.

PARSE
The  instruction is particularly powerful; it combines some useful string-handling functions. Its syntax is:

where origin specifies the source:


 * (arguments, at top level tail of command line)
 * (standard input, e.g. keyboard)
 * (Rexx data queue or standard input)
 * (info on how program was executed)
 * (an expression) : the keyword   is required to indicate where the expression ends
 * (a variable)
 * (version/release number)

and template can be:


 * list of variables
 * column number delimiters
 * literal delimiters

is optional; if specified, data will be converted to upper case before parsing.

Examples:

Using a list of variables as template

displays the following:

First name is: John Last name is: Smith

Using a delimiter as template:

also displays the following:

First name is: John Last name is: Smith

Using column number delimiters:

displays the following:

Area code is: 202 Subscriber number is: 123-1234

A template can use a combination of variables, literal delimiters, and column number delimiters.

INTERPRET
The INTERPRET instruction evaluates its argument and treats its value as a Rexx statement. Sometimes INTERPRET is the clearest way to perform a task, but it is often used where clearer code is possible using, e.g.,.

The INTERPRET instruction is powerful and one of the major reasons why writing Rexx compilers is not trivial.

The other reasons being Rexx's (decimal) arbitrary precision arithmetic (including fuzzy comparisons), use of the PARSE statement with programmatic templates, stemmed arrays, and sparse arrays. -->

This displays 16 and exits. Because variable contents in Rexx are strings, including rational numbers with exponents and even entire programs, Rexx offers to interpret strings as evaluated expressions.

This feature could be used to pass functions as function parameters, such as passing SIN or COS to a procedure to calculate integrals.

Rexx offers only basic math functions like ABS, DIGITS, MAX, MIN, SIGN, RANDOM, and a complete set of hex plus binary conversions with bit operations. More complex functions like SIN had to be implemented from scratch or obtained from third party external libraries. Some external libraries, typically those implemented in traditional languages, did not support extended precision.

Later versions (non-classic) support  constructs. Together with the built-in function, CALL can be used in place of many cases of. This is a classic program:

A slightly more sophisticated "Rexx calculator":

is shorthand for. Likewise,  is shorthand for.

The power of the INTERPRET instruction had other uses. The Valour software package relied upon Rexx's interpretive ability to implement an object-oriented programming environment. Another use was found in an unreleased Westinghouse product called Time Machine that was able to fully recover following a fatal error.

SIGNAL
The SIGNAL instruction is intended for abnormal changes in the flow of control (see the next section). However it like the GOTO statement found in other languages can be misused creating difficult-to-read code.

Error handling and exceptions
It is possible in Rexx to intercept and deal with errors and other exceptions, using the SIGNAL instruction. There are seven system conditions: ERROR, FAILURE, HALT, NOVALUE, NOTREADY, LOSTDIGITS and SYNTAX. Handling of each can be switched on and off in the source code as desired.

The following program will run until terminated by the user:

Virtually all serious Rexx programs contain  or a similar statement. This disables the "feature", where undefined variables get their own (upper case) name as value. The status of a variable can be checked with the built-in function  returning VAR for defined variables.

Function  can be used to get the value of variables without triggering a NOVALUE condition, but its main purpose is to read and set environment variables, similar to POSIX   and.

Conditions
When a condition is handled by, the   and   system variables can be analyzed to understand the situation. RC contains the Rexx error code and SIGL contains the line number where the error arose.

Beginning with Rexx version 4 conditions can get names, and there's also a  construct. That's handy if external functions do not necessarily exist: