# Rexx Programming/Introduction/syntax
## Syntax
### Looping

The loop control structure in Rexx begins with a DO and ends with an END but comes in several varieties. NetRexx uses the keyword LOOP instead of DO for looping, while ooRexx treats LOOP and DO 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 leave instruction (which is the normal way to exit a "forever" loop), or can short-circuit it with the iterate instruction.
The do while and do until forms are equivalent to:
and:

### Conditionals

Testing conditions with IF:
The ELSE clause is optional.
Or, to put it more concisely:
For single instructions, DO and END can also be omitted:
Or, in another concise version:
Indentation is optional, but it helps improve the readability.

### Testing for multiple conditions

SELECT is Rexx's CASE structure, like many other constructs derived from PL/I:
NOP 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 stem.3 would produce 'Unknown'.
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 stem.0 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 dictionary.0 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.

### Keyword instructions
#### PARSE

The PARSE instruction is particularly powerful; it combines some useful string-handling functions. Its syntax is:
where origin specifies the source:
* arg (arguments, at top level tail of command line)
* linein (standard input, e.g. keyboard)
* pull (Rexx data queue or standard input)
* source (info on how program was executed)
* value (an expression) with: the keyword with is required to indicate where the expression ends
* var (a variable)
* version (version/release number)
and template can be:
* list of variables
* column number delimiters
* literal delimiters
upper is optional; if specified, data will be converted to upper case before parsing.
Examples:
Using a list of variables as template
displays the following:
Using a delimiter as template:
also displays the following:
Using column number delimiters:
displays the following:
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., value.
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 CALL variable constructs. Together with the built-in function VALUE, CALL can be used in place of many cases of INTERPRET. This is a classic program:
A slightly more sophisticated "Rexx calculator":
PULL is shorthand for parse upper pull. Likewise, ARG is shorthand for parse upper arg.
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 signal on novalue 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 SYMBOL returning VAR for defined variables.
Function VALUE 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 getenv and putenv.

#### Conditions

| ERROR | Positive RC from a system command |
| FAILURE | Negative RC for a system command (e.g. command doesn't exist) |
| HALT | Abnormal termination |
| NOVALUE | An unset variable was referenced |
| NOTREADY | Input or output error (e.g. read attempts beyond end of file) |
| SYNTAX | Invalid program syntax, or some other error condition |
| LOSTDIGITS | Significant digits are lost (ANSI Rexx, not in TRL second edition) |
When a condition is handled by SIGNAL ON, the SIGL and RC 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 CALL ON construct. That's handy if external functions do not necessarily exist: