Rexx Programming/How to Rexx/if

From Wikibooks, open books for an open world
Jump to: navigation, search

The if conditional construct allows a statement or block of code to be conditionally executed based on the result of a boolean expression. The branch within the condition, only gets executed if the expression evaluates to a true value. The if condition its simplest form is:

if CONDITION then STATEMENT[edit]

For example:

if guess = 6 then
  say "Wow! That was a lucky guess."

There is no endif cocomponent[edit]

Note that rexx programming language syntax does not utilize endif as a syntactical cocomponent. The endif placed in a comment in example below are for cosmetic purposes only for aiding indentation. These comments are ignored by the rexx interpreter.

if guess = 6 then
  say "Wow! That was a lucky guess."
else
  say "Sorry! That was not the right number."
/* endif */


Using the do construct to conditionally execute a branch containing more than one statement[edit]

In order to conditionally execute more than one statement within a conditional branch, it is necessary to enclose the statements within a do and end block:

if guess = 6 then
  do
    say "Wow! That was a lucky guess."    /* Multiple statements in a do block */
    prize = 30000
  end

Nested conditional constructs[edit]

It is possible to created nested if structures:

if age > 79 then
  say "Wow! You are so old that you are almost antique!"
else
  if age >=65 then
    say "You are a pensioner"
  else
    if age > 21 then
      say "You have the key to the door"
    else
      say "You are just a youngster"
    /* endif */
  /* endif */
/* endif */