Skip to main content

Operator Precedence

Operator Precedence

Operator precedence in ObjectScript is strictly left-to-right; within an expression operations are performed in the order in which they appear. This is different from other languages in which certain operators have higher precedence than others. You can use explicit parentheses within an expression to force certain operations to be carried ahead of others.

 WRITE "1 + 2 * 3 = ", 1 + 2 * 3,!  // returns 9
 WRITE "2 * 3 + 1 = ", 2 * 3 + 1,!  // returns 7
 WRITE "1 + (2 * 3) = ", 1 + (2 * 3),!  // returns 7
 WRITE "2 * (3 + 1) = ", 2 * (3 + 1),!  // returns 8

Note that in InterSystems SQL operator precedence is configurable, and may (or may not) match the operator precedence in ObjectScript.

Unary Negative Operators

ObjectScript gives the unary negative operator precedence over the binary arithmetic operators. ObjectScript first scans a numeric expression and performs any unary negative operations. Then, ObjectScript evaluates the expression and produces a result.

  WRITE -123 - 3,!       // returns -126
  WRITE -123 + -3,!      // returns -126
  WRITE -(123 - 3),!     // returns -120

Parentheses and Precedence

You can change the order of evaluation by nesting expressions within each other with matching parentheses. The parentheses group the enclosed expressions (both arithmetic and relational) and control the order in which ObjectScript performs operations on the expressions. Consider the following expression:

  SET TorF = ((4 + 7) > (6 + 6)) // False (0)
  WRITE TorF

Here, because of the parentheses, four and seven are added, as are six and six; this results in the logical expression 11 > 12, which is false. Compare this to:

  SET Value = (4 + 7 > 6 + 6) // 7
  WRITE Value

In this case, precedence proceeds from left to right, so four and seven are added. Their sum, eleven, is compared to six; since eleven is greater than six, the result of this logical operation is one (TRUE). One is then added to six, and the result is seven.

Note that the precedence even determines the result type, since the first expression’s final operation results in a boolean and the second expression’s final operation results in a numeric.

The following example shows multiple levels of nesting:

 WRITE 1+2*3-4*5,!  // returns 25
 WRITE 1+(2*3)-4*5,!  // returns 15
 WRITE 1+(2*(3-4))*5,!  // returns -5
 WRITE 1+(((2*3)-4)*5),!  // returns 11

Precedence from the innermost nested expression and proceeds out level by level, evaluating left to right at each level.

Tip:

For all but the simplest ObjectScript expressions, it is good practice to fully parenthesize expressions. This is to eliminate any ambiguity about the order of evaluation and to also eliminate any future questions about the original intention of the code.

For example, because the && operator, like all operators, is subject to left-to-right precedence, the final statement in the following code fragment evaluates to 0:

 SET x = 3
 SET y = 2
 IF x && y = 2 {
   WRITE "True",! } 
 ELSE {
   WRITE "False",! }

This is because the evaluation occurs as follows:

  1. The first action is to check if x is defined and has a non-zero value. Since x equals 3, evaluation continues.

  2. Next, there is a check if y is defined and has a non-zero value. Since y equals 2, evaluation continues.

  3. Next, the value of 3 && 2 is evaluated. Since neither 3 nor 2 equal 0, this expression is true and evaluates to 1.

  4. The next action is to compare the returned value to 2. Since 1 does not equal 2, this evaluation returns 0.

For those accustomed to many programming languages, this is an unexpected result. If the intent is to return True if x is defined with a non-zero value and if y equals 2, then parentheses are required:

 SET x = 3
 SET y = 2
 IF x && (y = 2) {
   WRITE "True",! } 
 ELSE {
   WRITE "False",! }

Functions and Precedence

Some types of expressions, such as functions, can have side effects. Suppose you have the following logical expression:

 IF var1 = ($$ONE + (var2 * 5)) {
    DO ^Test 
 }

ObjectScript first evaluates var1, then the function $$ONE, then var2. It then multiplies var2 by 5. Finally, ObjectScript tests to see if the result of the addition is equal to the value in var1. If it is, it executes the DO command to call the Test routine.

As another example, consider the following logical expression:

  SET var8=25,var7=23
  IF var8 = 25 * (var7 < 24) {
    WRITE !,"True" }
  ELSE {
    WRITE !,"False" }

ObjectScript evaluates expressions strictly left-to-right. The programmer must use parentheses to establish any precedence. In this case, ObjectScript first evaluates var8=25, resulting in 1. It then multiplies this 1 by the results of the expression in parentheses. Because var7 is less than 24, the expression in parentheses evaluates to 1. Therefore, ObjectScript multiplies 1 * 1, resulting in 1 (true).

FeedbackOpens in a new tab