Expressions are used in:
The general form of an expression is one of the following:
| Form: | Examples: | ||||
| term | 42 'red' isPrime.x.y |
||||
| prefixOperator term | - 42 \ isPrime |
||||
| term operator term |
|
Expression terms can be one of the following:
| string | 'open sesame' "This isn't a numeric value" |
| hexadecimal string | '2 2bad'x |
| binary string | '1 01'b |
| symbol | 42 1.23e+42 ' + 7.9E42 ' first_name isPrime.x.y isPrime. |
| function result | left( 'abracadabra', 4 ) |
| parenthesized expression | (2 + 2) (2 + (6 * 3)) ('your name is' name) |
Furthermore, wherever a term can appear an expression can appear!
Here is an assignment instruction with an intricate expression:
vector = left( ' ', 8 * day1no ) || ,
overlay( ' > 'right( dayofmon, 2 )' < ' , ,
left( ' 1 2 3 4 5 ' || ,
' 6 7 8 9 10 ' || ,
' 11 12 13 14 15 ' || ,
' 16 17 18 19 20 ' || ,
' 21 22 23 24 25 ' || ,
' 26 27 28 29 30 ' || ,
' 31 ', (daysinmo * 8) ), ,
1 + ((dayofmon-1) * 8) )
|
| (space) | date() time() | the space between the functions is an operator |
| (abuttal) | date() || time() | the '||' operator is an explicit abuttal operator |
| (abuttal) | date()' at 'time() | the quoted string is implicitly concatenated to the adjacent terms |
Rexx has two fundamental string concatenation operators.
Click here for a detailed description of string concatenation operators.
Numeric terms can be processed with the following operators:
| + | Add | 21 + 21 | |
| - | Subtract | 44 - 2 | |
| * | Multiply | 6 * 7 | |
| / | Divide | 84 / 2 | |
| % | Integer divide | 85 % 2 | |
| // | Remainder | 42 // 50 | similar to modulo but the result can be negative |
| ** | Power (exponent) | 2 ** 4 | |
| Prefix + | (0 + number) | + 42 | |
| Prefix - | (0 - number) | - 42 |
Comparison operators compare two terms. The result of the comparison is '1' when the terms are equal, and '0' otherwise. There are two types of comparison operators -- strict and normal.
In normal comparisons, strings are compared for the length of the shorter term, and trailing spaces are ignored. The collating order of characters is implementation-dependent. There are significant differences between the ordering of characters in Ascii environments, versus EBCDIC.
When both terms in a normal comparison are numeric, then a numeric comparison is performed. '7' is less than '11' in a normal comparison.
In strict comparisons, trailing spaces are significant. Strict comparisons always compare string values, even though both terms are numeric. '7' is greater than '11' in a strict comparison!
| = | equal |
| ¬=, \=, ><, <> | not equal |
| > | greater than |
| < | less than |
| >=, ¬<, \< | greater than or equal; not less than |
| <=, ¬>, \> | less than or equal; not greater than |
Note: The ¬ operator character may be absent in Ascii implementations. The backslash character (\) is now preferred.
Strict comparison operators are similar to normal comparison operators, but has doubled operators.
| == | strictly equal |
| ¬==, \== | strictly not equal |
| >> | strictly greater than |
| << | strictly less than |
| >>=, ¬<<, \<< | strictly greater than or equal; strictly not less than |
| <<=, ¬>>, \>> | strictly less than or equal; strictly not greater than |
Logical operators transform the boolean sense of one or two terms. A term is considered to be false if it is strictly equal to '0'. A term is considered to be true if it is strictly equal to '1'. Any values other than '0' or '1' are invalid, and cause error #34 (logical value not 0 or 1) to be raised.
| & | And | returns '1' when both terms are true, and '0' otherwise |
| | | Or | returns '1' when either term is true, and '0' otherwise |
| && | Exclusive or | returns '1' when one of the terms is true (but not both), and '0' otherwise |
| Prefix ¬, Prefix \ | Logical not | returns '1' when the term is '0', and '0' when the term is '1'. |
Boolean expressions are used in the condition clauses of IF, DO, and WHEN instructions,
As mentioned above, the result of a boolean expression is considered to be false if it is strictly equal to '0'. A term is considered to be true if it is strictly equal to '1'. Any values other than '0' or '1' are invalid, and cause error #34 (logical value not 0 or 1) to be raised.
Some examples are helpful.
| if number < limit then ... | ok, hopefully both variables have numeric values |
| when result then ... | oops, what if result is not exactly '0' or '1' |
| do while lines( ) | oops, you probably want to do: lines( ) > 0 instead |