Product SiteDocumentation Site

1.11.3. Parentheses and Operator Precedence

Expression evaluation is from left to right; parentheses and operator precedence modify this:
  • When parentheses are encountered—other than those that identify the arguments on messages (see Section 1.11.4, “Message Terms”) and function calls—the entire subexpression between the parentheses is evaluated immediately when the term is required.
  • When the sequence
    term1 operator1 term2 operator2 term3
    
    is encountered, and operator2 has precedence over operator1, the subexpression (term2 operator2 term3) is evaluated first.
    Note, however, that individual terms are evaluated from left to right in the expression (that is, as soon as they are encountered). The precedence rules affect only the order of operations.
For example, * (multiply) has a higher priority than + (add), so 3+2*5 evaluates to 13 (rather than the 25 that would result if a strict left-to-right evaluation occurred). To force the addition to occur before the multiplication, you could rewrite the expression as (3+2)*5. Adding the parentheses makes the first three tokens a subexpression. Similarly, the expression -3**2 evaluates to 9 (instead of -9) because the prefix minus operator has a higher priority than the power operator.
The order of precedence of the operators is (highest at the top):
~   ~~\ (message send)
+   -   ¬   \ (prefix operators)
** (power)
*   /   %   // (multiply and divide)
+   - (add and subtract)
(blank)   ||   (abuttal) (concatenation with or without blank)
=   >   < (comparison operators)
==   >>   <<  
\=   ¬=  
><   <>  
\>   ¬>  
\<   ¬<  
\==   ¬==  
\>>   ¬>>  
\<<   ¬<<  
>=   >>=  
<=   <<=  
& (and)
|   && (or, exclusive or)
Suppose the symbol A is a variable whose value is 3, DAY is a variable whose value is Monday, and other variables are uninitialized. Then:

Example 1.16. Arithmetic


A+5                  ->    "8"
A-4*2                ->    "-5"
A/2                  ->    "1.5"
0.5**2               ->    "0.25"
(A+1)>7              ->    "0"         /* that is, False */
" "=""               ->    "1"         /* that is, True  */
" "==""              ->    "0"         /* that is, False */
" "\==""             ->    "1"
/* that is, True  */
(A+1)*3=12           ->    "1"         /* that is, True  */
"077">"11"           ->    "1"         /* that is, True  */
"077" >> "11"        ->    "0"         /* that is, False */
"abc" >> "ab"        ->    "1"         /* that is, True  */
"abc" << "abd"       ->    "1"         /* that is, True  */
"ab " << "abd"       ->    "1"         /* that is, True  */
Today is Day         ->    "TODAY IS Monday"
"If it is" day       ->    "If it is Monday"
Substr(Day,2,3)      ->    "ond"    /* Substr is a function */
"!"xxx"!"            ->    "!XXX!"


Note

The Rexx order of precedence usually causes no difficulty because it is the same as in conventional algebra and other computer languages. There are two differences from common notations:
  • The prefix minus operator always has a higher priority than the power operator.
  • Power operators (like other operators) are evaluated from left to right.
For example:

Example 1.17. Arithmetic

-3**2     ==  9  /* not -9  */
-(2+1)**2 ==  9  /* not -9  */
2**2**3   == 64  /* not 256 */