Using Expressions

An expression is something that needs to be evaluated and consists of numbers, variables, or strings, and zero or more operators. The operators determine the kind of evaluation to do on the numbers, variables, and strings. There are four types of operators: arithmetic, comparison, logical, and concatenation.

Arithmetic Operators

Arithmetic operators work on valid numeric constants or on variables that represent valid numeric constants.

Types of Numeric Constants
12
A whole number has no decimal point or commas. Results of arithmetic operations with whole numbers can contain a maximum of nine digits unless you override this default by using the NUMERIC DIGITS instruction. For information about the NUMERIC DIGITS instruction, see section section NUMERIC. Examples of whole numbers are:
123456789   0   91221  999
12.5
A decimal number includes a decimal point. Results of arithmetic operations with decimal numbers are limited to a total maximum of nine digits (NUMERIC DIGITS default) before and after the decimal. Examples of decimal numbers are:
123456.789  0.888888888
1.25E2
A floating point number in exponential notation, is said to be in scientific notation. The number after the "E" represents the number of places the decimal point moves. Thus 1.25E2 (also written as 1.25E+2) moves the decimal point to the right two places and results in 125. When an "E" is followed by a minus (-), the decimal point moves to the left. For example, 1.25E-2 is .0125.

You can use floating point numbers to represent very large or very small numbers. For more information about scientific notation (floating point numbers), see section Exponential Notation.

-12
A signed number with a minus (-) next to the number represents a negative value. A signed number with a plus (+) next to the number represents a positive value. When a number has no sign, it is processed as if it has a positive value.

The arithmetic operators you can use are:

Operator
Meaning
+
Add
-
Subtract
*
Multiply
/
Divide
%
Divide and return a whole number without a remainder
//
Divide and return the remainder only
**
Raise a number to a whole number power
-number
(Prefix -) Same as the subtraction 0 - number
+number
(Prefix +) Same as the addition 0 + number

Using numeric constants and arithmetic operators, you can write arithmetic expressions such as:

7 + 2                         /*  result is 9         */
7 - 2                         /*  result is 5         */
7 * 2                         /*  result is 14        */
7 ** 2                        /*  result is 49        */
7 ** 2.5                      /*  result is an error  */

Division

Notice that three operators represent division. Each operator computes the result of a division expression in a different way.

/
Divide and express the answer possibly as a decimal number. For example:
7 / 2                         /* result is 3.5  */
6 / 2                         /* result is 3    */
%
Divide and express the answer as a whole number. The remainder is ignored. For example:
7 % 2                         /* result is 3    */
//
Divide and express the answer as the remainder only. For example:
7 // 2                        /* result is 1    */

Order of Evaluation

When you have more than one operator in an arithmetic expression, the order of numbers and operators can be critical. For example, in the following expression, which operation does the language processor perform first?

7 + 2 * (9 / 3) - 1

Proceeding from left to right, the language processor evaluates the expression as follows:

Arithmetic operator priority is as follows, with the highest first:

Table 1. Arithmetic Operator Priority
- + Prefix operators
** Power (exponential)
* / % // Multiplication and division
+ - Addition and subtraction

Thus, the preceding example would be evaluated in the following order:

  1. Expression in parentheses
       7 + 2 * (9 / 3) - 1
                \___/
                  3
  2. Multiplication
       7 + 2 * 3 - 1
           \___/
             6
  3. Addition and subtraction from left to right
       7 + 6 - 1 = 12

Using Arithmetic Expressions

You can use arithmetic expressions in a program many different ways. The following example uses several arithmetic operators to round and remove extra decimal places from a dollar and cents value.

Figure 11. Example Using Arithmetic Expressions
/****************************** REXX *********************************/
/* This program computes the total price of an item including sales  */
/* tax, rounded to two decimal places.  The cost and percent of the  */
/* tax (expressed as a decimal number) are passed to the program     */
/* when you run it.                                                  */
/*********************************************************************/

 PARSE ARG cost percent_tax

 total = cost + (cost * percent_tax)      /* Add tax to cost.        */
 price = ((total * 100 + .5) % 1) / 100   /* Round and remove extra  */
                                          /* decimal places.         */
 SAY 'Your total cost is £'price'.'

Exercises--Calculating Arithmetic Expressions

  1. What line of output does the following program produce?
    /****************************** REXX ******************************/
      pa = 1
      ma = 1
      kids = 3
      SAY "There are" pa + ma + kids "people in this family."
  2. What is the value of:
    1. 6 - 4 + 1
    2. 6 - (4 + 1)
    3. 6 * 4 + 2
    4. 6 * (4 + 2)
    5. 24 % 5 / 2

ANSWERS

  1. There are 5 people in this family.
  2. The values are as follows:
    1. 3
    2. 1
    3. 26
    4. 36
    5. 2

Comparison Operators

Expressions that use comparison operators do not return a number value as do arithmetic expressions. Comparison expressions return either 1, which represents true, or 0, which represents false.

Comparison operators can compare numbers or strings and perform evaluations, such as:

For example, if A = 4 and Z = 3, then the results of the previous comparison questions are:

(A = Z)  Does 4 = 3?        0 (False)
(A > Z)  Is 4 > 3?             1 (True)
(A < Z)  Is 4 < 3?             0 (False)

The more commonly used comparison operators are as follows:

Operator
Meaning
=
Equal
==
Strictly Equal
\ =
Not equal
\ ==
Not strictly equal
>
Greater than
<
Less than
> <
Greater than or less than (same as not equal)
> =
Greater than or equal to
\ <
Not less than
< =
Less than or equal to
\ >
Not greater than
Note:
The NOT character (¬) is synonymous with the backslash (\). You can use the two characters interchangeably according to availability and personal preference. This book uses the backslash (\) character.

The Strictly Equal and Equal Operators

When two expressions are strictly equal, everything including the blanks and case (when the expressions are characters) is exactly the same.

When two expressions are equal, they are resolved to be the same. The following expressions are all true.

'WORD' = word              /* returns 1 */
'word ' \== word           /* returns 1 */
'word' == 'word'           /* returns 1 */
 4e2 \== 400               /* returns 1 */
 4e2 \= 100                /* returns 1 */

Using Comparison Expressions

You often use a comparison expression in an IF...THEN...ELSE instruction. The following example uses an IF...THEN...ELSE instruction to compare two values. For more information about this instruction, see section IF...THEN...ELSE Instructions.

Figure 12. Example Using a Comparison Expression
/****************************** REXX *********************************/
/* This program compares what you paid for lunch for two             */
/* days in a row and then comments on the comparison.                */
/*********************************************************************/

PARSE PULL yesterday    /* Gets yesterday's price from input stream */


PARSE PULL today            /*  Gets today's price  */


IF today > yesterday THEN   /* lunch cost increased */
   SAY "Today's lunch cost more than yesterday's."

ELSE            /* lunch cost remained the same or decreased */
   SAY "Today's lunch cost the same or less than yesterday's."

Exercises - Using Comparison Expressions

  1. Based on the preceding example of using a comparison expression, what result does the language processor produce from the following lunch costs?
    Yesterday's Lunch
    Today's Lunch
     
     
    4.42
    3.75
    3.50
    3.50
    3.75
    4.42
  2. What is the result (0 or 1) of the following expressions?
    1. "Apples" = "Oranges"
    2. " Apples" = "Apples"
    3. " Apples" == "Apples"
    4. 100 = 1E2
    5. 100 \= 1E2
    6. 100 \== 1E2

ANSWERS

  1. The language processor produces the following sentences:
    1. Today's lunch cost the same or less than yesterday's.
    2. Today's lunch cost the same or less than yesterday's.
    3. Today's lunch cost more than yesterday's.
  2. The expressions result in the following. Remember 0 is false and 1 is true.
    1. 0
    2. 1
    3. 0 (The first " Apples" has a space.)
    4. 1
    5. 0
    6. 1

Logical (Boolean) Operators

Logical expressions, like comparison expressions, return 1 (true) or 0 (false) when processed. Logical operators combine two comparisons and return 1 or 0 depending on the results of the comparisons.

The logical operators are:

Operator
Meaning
&
AND

Returns 1 if both comparisons are true. For example:

(4 > 2) & (a = a)   /* true, so result is 1  */

(2 > 4) & (a = a)   /* false, so result is 0 */
|
Inclusive OR

Returns 1 if at least one comparison is true. For example:

(4 > 2) | (5 = 3)   /* at least one is true, so result is 1 */

(2 > 4) | (5 = 3)   /* neither one is true, so result is 0  */
&&
Exclusive OR

Returns 1 if only one comparison (but not both) is true. For example:

(4 > 2) && (5 = 3)  /* only one is true, so result is 1    */

(4 > 2) && (5 = 5)  /* both are true, so result is 0       */

(2 > 4) && (5 = 3)  /* neither one is true, so result is 0 */
Prefix \,¬
Logical NOT

Negates--returning the opposite response. For example:

\ 0                 /* opposite of 0, so result is 1    */

\ (4 > 2)           /* opposite of true, so result is 0 */

Using Logical Expressions

You can use logical expressions in complex conditional instructions and as checkpoints to screen unwanted conditions. When you have a series of logical expressions, for clarification, use one or more sets of parentheses to enclose each expression.

IF ((A < B) | (J < D)) & ((M = Q) | (M = D)) THEN ....

The following example uses logical operators to make a decision.

Figure 13. Example Using Logical Expressions
/****************************** REXX ********************************/
/* This program receives arguments for a complex logical expression */
/* that determines whether a person should go skiing.  The first    */
/* argument is a season and the other two can be 'yes' or 'no'.     */
/********************************************************************/


 PARSE ARG season snowing broken_leg

 IF ((season = 'WINTER') | (snowing ='YES')) & (broken_leg ='NO')
    THEN SAY 'Go skiing.'
 ELSE
    SAY 'Stay home.'

When arguments passed to this example are SPRING YES NO, the IF clause translates as follows:

IF ((season = 'WINTER') | (snowing ='YES')) & (broken_leg ='NO') THEN
     \______________/      \____________/       \_____________/
          false                 true                 true
             \___________________/                    /
                    true                             /
                      \_____________________________/
                                    true

As a result, when you run the program, it produces the result:

Go skiing.

Exercises - Using Logical Expressions

A student applying to colleges has decided to evaluate them according to the following specifications:

IF  (inexpensive | scholarship) & (reputable | nearby)  THEN
   SAY  "I'll consider it."
ELSE
   SAY  "Forget it!"

A college is inexpensive, did not offer a scholarship, is reputable, but is more than 1000 miles away. Should the student apply?

ANSWER

Yes. The conditional instruction works out as follows:

IF  (inexpensive | scholarship) & (reputable | nearby)  THEN ...
     \__________/ \___________/   \_________/ \______/
        true          false          true      false
           \___________/               \_________/
               true                       true
                 \_________________________/
                            true

Concatenation Operators

Concatenation operators combine two terms into one. The terms can be strings, variables, expressions, or constants. Concatenation can be significant in formatting output.

The operators that indicate how to join two terms are as follows:

Operator
Meaning
blank
Concatenates terms and places one blank between them. If more than one blank separates terms, this becomes a single blank. For example:
SAY true      blue          /* result is TRUE BLUE */
||
Concatenates terms with no blanks between them. For example:
(8 / 2)||(3 * 3)            /* result is 49  */
abuttal
Concatenates terms with no blanks between them. For example:
per_cent'%'                /* if per_cent = 50, result
                                is 50%  */
You can use abuttal only with terms that are of different types, such as a literal string and a symbol, or when only a comment separates two terms.

Using Concatenation Operators

One way to format output is to use variables and concatenation operators as in the following example.

Figure 14. Example Using Concatenation Operators
/****************************** REXX *********************************/
/* This program formats data into columns for output.                */
/*********************************************************************/
  sport = 'base'
  equipment = 'ball'
  column = '         '
  cost = 5

  SAY sport||equipment column '£' cost

The result of this example is:

baseball         £ 5

A more sophisticated way to format information is with parsing and templates. Information about parsing appears in section Parsing Data.

Priority of Operators

When more than one type of operator appears in an expression, what operation does the language processor do first?

IF (A > 7**B) & (B < 3)

Like the priority of operators for the arithmetic operators, there is an overall priority that includes all operators. The priority of operators is as follows with the highest first.

Table 2. Overall Operator Priority
\ or ¬ - + Prefix operators
** Power (exponential)
* / % // Multiply and divide
+ - Add and subtract
blank || abuttal Concatenation operators
== = >< and so on Comparison operators
& Logical AND
| && Inclusive OR and exclusive OR

Thus, given the following values

the language processor would evaluate the previous example

    IF (A > 7**B) & (B < 3)

as follows:

  1. Evaluate what is inside the first set of parentheses.
    1. Evaluate A to 8.
    2. Evaluate B to 2.
    3. Evaluate 7**2.
    4. Evaluate 8 > 49 is false (0).
  2. Evaluate what is inside the next set of parentheses.
    1. Evaluate B to 2.
    2. Evaluate 2 < 3 is true (1).
  3. Evaluate 0 & 1 is 0

Exercises - Priority of Operators

  1. What are the answers to the following examples?
    1. 22 + (12 * 1)
    2. -6 / -2 > (45 % 7 / 2) - 1
    3. 10 * 2 - (5 + 1) // 5 * 2 + 15 - 1
  2. In the example of the student and the college from the previous exercise on page Exercises - Using Logical Expressions, if the parentheses were removed from the student's formula, what would be the outcome for the college?
    IF  inexpensive | scholarship & reputable | nearby  THEN
       SAY  "I'll consider it."
    ELSE
       SAY  "Forget it!"

    Remember the college is inexpensive, did not offer a scholarship, is reputable, but is 1000 miles away.

ANSWERS

  1. The results are as follows:
    1. 34 (22 + 12 = 34)
    2. 1 (true) (3 > 3 - 1)
    3. 32 (20 - 2 + 15 - 1)
  2. I'll consider it.

    The & operator has priority, as follows, but the outcome is the same as the previous version with the parentheses.

    IF  inexpensive | scholarship & reputable | nearby  THEN
        \_________/   \_________/   \_______/   \____/
           true          false        true      false
             \             \___________/         /
              \                false            /
               \_________________/             /
                      true                    /
                        \____________________/
                                true