Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This

A Simple Example

Here’s an easy first example program to show what Rexx looks like:

/* GUESSING GAME -- try guessing the randomly-generated number */

do while end_this_game <> ’N’

   the_number = random(1,5)  /* generate the random number */

   say "I’m thinking of a number between 1 and 5, what is it?"

   pull your_guess
   say "You entered: " your_guess

   if your_guess = the_number then
     say ’You guessed it! The number was: ’ the_number
   else do
     say ’Sorry, the number was: ’ the_number
     say ’We should have bet on this!’

   say ’Try again? [Y or N]: ’
   pull end_this_game

You can see that Rexx requires absolutely minimal syntax. It’s a freeform language, so you can space between language elements and indent lines however you like. Rexx is also not case sensitive, so you can refer to a variable like end_this_game as End_This_Game or END_THIS_GAME. It doesn’t matter; Rexx treats it as the same variable.

Enclose comments between the delimiters (/* and */). Comments can reside on separate lines or appear on lines that contain executable statements. Comments can even span lines.

Variables don’t have to be initialized, as the first do while statement in the example shows. An uninitialized variable defaults to its name in uppercase, so in that first do while, the variable end_this_game contains the value END_THIS_GAME. The first time through the loop, the script continues because the literal value N is not equal to the string value END_THIS_GAME. Rexx features many such quick and convenient coding shortcuts—though, as when using any "power" language, you can always easily override any of those shortcuts.

In larger Rexx programs, you’ll probably want to initialize all variables (unlike the shortcut I took here). This is fine—Rexx is flexible. Rexx provides a mechanism to identify or trap the situation when you unintentionally refer to an uninitialized variable. This is called the novalue exception or condition. Rexx provides a number of exception conditions you can trap, as shown in Table 2.

Table 2 Exception Conditions You Can Trap

Exception Condition or Trap



Raised by references to uninitialized variables


Raised when a command issued to an external environment indicates an error upon return


Raised when a command issued to an external environment fails


Raised by an external interrupt to the script (such as Ctrl+C)


Raised by an unready I/O device


Raised by a syntax or runtime error in the script


Raised when an arithmetic operation loses digits

These exception conditions provide an easy way to divert the script to special routines coded to handle problems. (Rexx has other ways to manage errors too, of course.)

Notice that several of the conditions refer to errors that occur when your script issues commands to external environments (such as the operating system or other programming interfaces). Strong at manipulating strings, Rexx is especially useful for issuing commands to operating systems, and managing their results.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.