You use a conditional instruction whenever you want to check if some expression is true, and then execute other REXX instructions only if that expression is true.

A conditional's expression is simply any question such as "is this particular variable's value equal to this other variable's value?", although you don't notate a REXX conditional expression in quite such explicit English. For example, here's how we write an expression in which we ask if the value of a variable named 'My_Variable' is greater than 10:

IF My_Variable > 10
A conditional instruction consists of a REXX conditional keyword, followed by the conditional expression, and then the rest of the conditional (which is usually another conditional keyword, followed by an instruction -- a "sub-instruction" if you will). In our above example, IF is the conditional keyword, and My_Variable > 10 is the expression. Right now, we're missing the rest of the conditional instruction. We'll get to that in a moment.

The expression must evaluate to either true (ie, 1) or false (0). REXX automatically evaluates the expression, and if the expression is true, then the rest of the conditional is executed. Otherwise, the rest of the conditional is skipped.

For example, above, REXX was asked the question "Is My_Variable's value greater than 10?". REXX will check My_Variable's value to see if it is indeed greater than 10. And then, REXX will evaluate this as true if My_Variable's value is indeed greater than 10, or false if not.

As you can see, a conditional expression often consists of a comparison of two items. Those items may be variables, or literal strings (including numeric strings), or a mixture of the two (as our above example of a variable and a numeric string), or even the returned value of some REXX function or subroutine.

How do you specify the rest of the conditional instruction? You use another conditional keyword, followed by one (or more, as we'll see later) instructions that should be executed only if the expression is true.

One possible conditional keyword that we can use is THEN. For example, the following script checks if the value of a variable named My_Variable is greater than 10, and if true, then the literal string It's greater than 10 is printed to the screen.

/* Check if My_Variable is greater than 10, and then say so if true */
IF My_Variable > 10 THEN SAY "It's greater than 10."
As you can see, THEN SAY "It's greater than 10." is the rest of our conditional instruction. This part gets executed only if it's true that My_Variable > 10.

Let's examine that remaining part of the conditional. THEN is the second keyword, and SAY "It's greater than 10." is that "sub-instruction" we referred to before (which also happens to begin with a keyword). The above instruction uses two REXX conditional keywords which work together; IF and THEN. IF is placed immediately before the expression (which in this case is the comparison of My_Variable to 10). THEN is placed before the sub-instruction that you want executed (which in this case is the SAY instruction) if the expression is true.

You'll note that I use the comparison operator > to represent "is greater than". There are several operators that you can use when comparing two items, as so:

="equal to"
<"less than"
>"greater than"
<="less than or equal to"
>="greater than or equal to"
<>"greater than or less than (ie, not equal to)"
\= or ^="not equal to"
\<"not less than"
\>"not greater than"

For example, let's say that I want to check if My_Variable is not equal to 10, and SAY "Not equal to 10" if that's true.

/* Check if My_Variable is not equal to 10, and then say so if true */
IF My_Variable \= 10 THEN SAY "Not equal to 10."
Here I check whether the value of the variable named My_Variable is greater than 5 plus the value of the variable named My_Other_Variable. If true, then I set My_Variable to 10 (ie, I perform an assignment instruction).
/* If My_Variable is greater than My_Other_Variable + 5,
   then set My_Variable to 10 */
IF My_Variable > My_Other_Variable + 5 THEN My_Variable = 10

String comparison

REXX compares strings (including variables that have string values rather than numeric values) on a character by character basis. Consider the following:

/* If My_Variable is greater than "hello",
   then say "It's true" */
My_Variable = "hell"
IF My_Variable >= "hello" THEN SAY "It's true."
First, REXX strips off any leading and trailing spaces from both items. Then, REXX compares the first character of the literal string "hello" with the first character of the value of My_Variable. Both characters are the letter "h", so the expression is true so far (ie, My_Variable is greater than or equal to "hello"). Next, the second characters of both strings are compared. These are both "e". The expression is still true. By the time that REXX gets around to comparing the "o" in "hello", it has run out of characters in the value of My_Variable, so it substitutes a blank space as the next character. Therefore, My_Variable is "less than" the literal string "hello" (ie, a blank space is considered to be less than the letter "o"), and the expression is ultimately false. The SAY instruction is not executed.

Let's look at another variation of the above.

/* If My_Variable is greater than "help", then say "It's true" */
My_Variable = "hell"
IF My_Variable >= "help" THEN SAY "It's true."
Again, REXX compares the strings on a character by character basis (after leading/trailing blanks are removed). The expression is true until REXX compares the "p" in "help" to the second "l" in "hell". It finds that "p" is greater than "l" (ie, "p" is the 16th letter in the alphabet whereas "l" is only the 12th letter), and therefore My_Variable is "less than" the literal string "help".

Upper case letters are considered to be less than lower case letters (ie, "A" is less than "a"), and in general, numeric digits are less than letters (ie, "3" is less than "A"). A blank space is less than letters or numeric characters.

You can also use the COMPARE() built-in function to compare two strings in order to determine if they are unequal, and if so, at what character position they are unequal.


Numeric versus Non-numeric values

When comparing two items, one of them can't be a numeric item, and the other non-numeric, or REXX will indicate a SYNTAX error. For example, the following will cause a REXX SYNTAX error, because My_Variable1 has a numeric value, and My_Variable2 does not.

My_Variable1 = 1
My_Variable2 = "hello"
IF My_Variable1 = My_Variable2 THEN SAY "They're equal."
To compare two items where one may be numeric and the other may not, use Strict Comparison Operators instead (and optionally use STRIP() to remove leading and trailing spaces).


In conclusion, the expression in a Conditional instruction can involve variables, literal strings, and/or returned values from functions or subroutines, but ultimately, REXX will evaluate the expression to be true (or false), and execute (or skip) the remainder of the Conditional accordingly.