After completing this chapter, you will be able to describe the following.
Rexx Operators
Types of Operators - Arithmetic, Comparison, Logical(Boolean), Concatenation
Rexx Expressions - Arithmetic, Comparison, Logical(Boolean), Concatenation
Operators Precedence
The operators determine the kind of calculation to be done on the numbers, variables, and strings. There are four types of operators: arithmetic, comparison, logical, and concatenation.
The arithmetic operators used in REXX numeric expressions are as follows.
Arithmetic operators work on valid numeric constants or on variables that represent valid numeric constants.
Operator | Meaning |
---|---|
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
% | Divide and return a whole number without a remainder |
// | Divide and return the whole number only |
** | Raise a number to a whole number power |
- | -number (Negate the number) |
+ | +number (Add the number to 0) |
Using numeric constants and arithmetic operators, you can write arithmetic expressions as follows.
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 */ |
You can use arithmetic expressions in an exec 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 exec computes the total price of an item including sales tax */ /* tax (expressed as a decimal number) are passed to the exec when */ /* it is run */ /**********************************************************************/ 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'.' |
Comparison operators can compare numbers or strings and ask questions, such as
Are the terms equal? (A = B)
Is the first term greater than the second? (A > B)
Is the first term less than the second? (A < B)
For example, if A = 4 and B = 3, then the results of the previous comparison questions are:
(A = B) Does 4 = 3? 0 (False)
(A > B) Is 4 > 3? 1 (True)
(A < B) Is 4 < 3? 0 (False)
The more commonly used comparison operators are as follows.
Operator | Meaning |
---|---|
== | Strictly Equal |
= | Equal |
\== | Not Strictly Equal |
\= | Not Equal |
> | Greater than |
< | Less than |
>< | Greater than or less than |
>= | 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 ("\"). The two characters may be used interchangeably according to availability and personal preference. This book uses the backslash ("\") character.
Often a comparison expression is used in IF/THEN/ELSE instructions. The following example uses an IF/THEN/ELSE instruction to compare two values. For more information about this instruction, see ―IF/THEN/ELSE Instructions.
/**********************************REXX********************************/ /* This exec compares what you paid for lunch for two days in a row */ /* and then comments on the comparison the exec when it is run */ /**********************************************************************/ SAY 'What did you spend for lunch yesterday? SAY 'Please do not include the dollar sign.' PARSE PULL last SAY 'What did you spend for lunch today?' SAY 'Please do not include the dollar sign.' PARSE PULL lunch IF lunch > last 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." |
The logical operators are as follows.
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 Returns the opposite response. For example: \ 0 /*opposite of 0, so result is 1 */ \ (4 > 2) /* opposite of true, so result is 0 */ |
Logical expressions are used in complex conditional instructions and can act 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 exec 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 exec, you see the message - 'Go skiing.'
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 | Concatenate terms and place one blank in between. Terms that are separated by more than one blank default to one blank when read. For example: SAY true blue /* result is TRUE BLUE */ |
|| | Concatenate terms and place no blanks in between. For example: (8 / 2)||(3 * 3) /* result is 49 */ |
abuttal | Concatenate terms and place no blanks in between. For example: per_cent'%' /* if per_cent = 50, result is 50% */ |
One way to format output is to use variables and concatenation operators as in the following example. A more sophisticated way to format information is with parsing and templates.
/****************************** REXX **********************************/ /* This exec 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
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) | (A||B = C) THEN ... |
Like the priority of operators within the arithmetic operators, there is an overall priority that includes all operators. The priority of operators is as follows with the highest first.
Prefix operators
Power (exponential)
Multiply and divide
Add and subtract
Concatenation operators
Comparison operators
Logical AND
Inclusive OR and exclusive OR
Thus the previous example presented again below
IF (A > 7**B) & (B < 3) | (A||B = C) THEN ... |
Given the following values, A=8, B=2, C=10. And, the result of the expersion is 0.
Various operators like arithmetic, logical, comparative and concatenation operators are available for developing applications.
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!