Sample REXX Language Program

The following program illustrates some of the REXX language statements:
/* The first line of a REXX exec must always be a comment.
   The comments can span more than one line.            */
    credits = 0
    do until credits > 5
    a = random(1,9)
    b = random(1,9)
    say "What is "  a "plus"  b  "?"
    pull answer  /* Place user's reply into answer */
    if answer = a + b
    then do
         credits = credits + 1; say "Correct."
         say "Your score is" credits
         end
    else
         say a "+" b "is" a+b
    end
    exit
 
This program repeatedly asks for the sum of two random numbers until it has accumulated six correct answers. The following describes the sequence of execution:
  • The comment delimiter /* on the first line indicates to CMS that this is a REXX program. This causes CMS to call the interpreter. The last comment line must end with */.
  • A value of 0 is assigned to the variable credits.
  • The lines from do until to the second end are repeatedly executed as long as the value of credits does not exceed 5.
  • Variables a and b are assigned random values in the range 1 to 9. The term random(1,9) is a built-in function; its arguments are the desired range in which the random number is to be generated. The REXX language has over fifty built-in functions. They are listed in z/VM: REXX/VM Reference.
  • The instruction say writes the values of a and b to the console along with the literals What is, plus, and ? in the order specified. One space is automatically inserted between separate literals or variables or both. If more than one space is required, it must be incorporated into a literal (as, for example, in What is ).
  • The instruction pull accepts the console reply into the variable answer. Comments in the REXX language can be included on the same lines as program statements.
    There are two forms of the PULL instruction:
    • The form PARSE UPPER PULL, which is normally abbreviated to PULL, translates everything read from the keyboard to upper case in the program.
    • The form PARSE PULL should be used if everything is required as is, without any translation.
  • In the statement if answer = a + b, the item to the right of the = sign can be an expression, in this case a + b.
  • When the test is true, more than one statement is to be executed. The do...end delimits these statements. If only one statement is to be executed, the delimiters are not required.
  • So far there has been only one REXX language statement per line. A line-end is considered to be an implied delimiter. However, if more than one statement is to be placed on a line, the delimiter ; can be used.
  • If a correct answer requires no action, if...then ; else... would be incorrect. A semicolon does not cause a null instruction to be executed; the no-operation instruction nop would have to be used, as in if...then nop else....
  • When the test fails, the else portion is executed. You can include the value of expressions (for example, a+b) in the data to be displayed on the console.

For full details on REXX instructions, see the z/VM: REXX/VM Reference.