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 work on valid numeric constants or on variables that represent valid numeric constants.
123456789 0 91221 999
123456.789 0.888888888
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.
The arithmetic operators you can use are:
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 */
Notice that three operators represent division. Each operator computes the result of a division expression in a different way.
7 / 2 /* result is 3.5 */
6 / 2 /* result is 3 */
7 % 2 /* result is 3 */
7 // 2 /* result is 1 */
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:
Operator symbol | Operator description |
---|---|
- + | Prefix operators |
** | Power (exponential) |
* / % // | Multiplication and division |
+ - | Addition and subtraction |
Thus, the preceding example would be evaluated in the following order:
7 + 2 * (9 / 3) - 1
\___/
3
7 + 2 * 3 - 1
\___/
6
7 + 6 - 1 = 12
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.
/****************************** 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'.'
/****************************** REXX ******************************/
pa = 1
ma = 1
kids = 3
SAY "There are" pa + ma + kids "people in this family."
ANSWERS
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:
The more commonly used comparison operators are as follows:
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 */
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.
/****************************** 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."
ANSWERS
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:
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 */
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 */
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 */
Negates--returning the opposite response. For example:
\ 0 /* opposite of 0, so result is 1 */
\ (4 > 2) /* opposite of true, so result is 0 */
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.
/****************************** 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.
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 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:
SAY true blue /* result is TRUE BLUE */
(8 / 2)||(3 * 3) /* result is 49 */
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.
One way to format output is to use variables and concatenation operators as in the following example.
/****************************** 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.
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.
Operator symbol | Operator description |
---|---|
\ 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:
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
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