Kilowatt Software L.L.C.
Classic Rexx Tutorial
Language Level 4.00 (TRL-2)

If instruction

The if instruction group is an important programming concept. The execution of one or more instructions is performed when a condition is true. An alternative series of instructions is optionally performed when the condition is false.

In Rexx, conditions are evaluated as boolean expressions. A boolean expression is true if the value is '1', and false if the value is '0'. Any other values are invalid.

The condition is concluded when the end of the if source line is encountered. The source line may have been continued across multiple lines. If the then keyword is encountered on the same source line, or continued source lines, the condition is immediately concluded. Thus, the then keyword is special and cannot be used within the context of the conditional expression.

Here are various forms of if instruction usage. These are presented using a style which has improved readability. Notice that the then keyword is placed on the same line as the if instruction. When present the optional do keyword is on the same line as the then or else keywords. The instructions associated with the then or else are indented. The corresponding end keyword is at the same level of indentation as the prior instructions.

Within the series of instructions, another if instruction group can be specified. This can introduce a difficulty. An else sequence binds to the nearest incomplete if instruction. Consider the following example:

When the selected color is 'RED', and the time is between noon and midnight, the above example shows:
  that's a hot color

When the selected color is 'RED', and the time is between midnight and noon, the above example shows:
  ok

When the selected color is not 'RED', nothing is displayed -- SURPRISE! This is because the else binds with the if instruction that tested the time, instead of binding with the if instruction that tested the color. You might be confused because the structure of the logic appears to connect the else with the first if. However, as mentioned above, the else sequence binds to the nearest incomplete if instruction.

The following shows how the logic is actually structured:

Other programming languages use isolated semicolons as empty instructions. In Rexx programs, isolated semicolons are not empty instructions. The NOP instruction is explicitly used for this purpose instead. For example, the following program is erroneous:

The correct way to program this in Rexx uses the NOP instruction instead.

A common programming idiom is a cascading series of if ... else if ... else ... instruction groups. These can have do ... end instruction sequences also. The following shows the structure of this idiom.

An alternative approach to the above cascade uses the select instruction instead. These can have do ... end instruction sequences also. The following shows the structure of this idiom when a select instruction group is used.


Kilowatt Software L.L.C. -- Classic Rexx Tutorial -- Back to top
Click here if you have any comments or questions regarding this tutorial

Last updated on: 11 Apr 2006