25.01.2015 Views

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introduction to Operators and Expressions<br />

5.1.1.1 Unary Negative Operators<br />

<strong>Caché</strong> <strong>ObjectScript</strong> gives the unary negative operator precedence over the binary arithmetic operators. <strong>Caché</strong> <strong>ObjectScript</strong><br />

first scans a numeric expression and performs any unary negative operations. Then, <strong>Caché</strong> <strong>ObjectScript</strong> evaluates the<br />

expression and produces a result.<br />

WRITE -123 - 3,! // returns -126<br />

WRITE -123 + -3,! // returns -126<br />

WRITE -(123 - 3),! // returns -120<br />

5.1.1.2 Parentheses and Precedence<br />

You can change the order of evaluation by nesting expressions within each other with matching parentheses. The parentheses<br />

group the enclosed expressions (both arithmetic and relational) and control the order in which <strong>Caché</strong> <strong>ObjectScript</strong> performs<br />

operations on the expressions. Consider the following expression:<br />

Set TorF = ((4 + 7) > (6 + 6)) // False (0)<br />

Write TorF<br />

Here, because of the parentheses, four and seven are added, as are six and six; this results in the logical expression 11 ><br />

12, which is false. Compare this to:<br />

Set Value = (4 + 7 > 6 + 6) // 7<br />

Write Value<br />

In this case, precedence proceeds from left to right, so four and seven are added. Their sum, eleven, is compared to six;<br />

since eleven is greater than six, the result of this logical operation is one (TRUE). One is then added to six, and the result<br />

is seven.<br />

Note that the precedence even determines the result type, since the first expression’s final operation results in a boolean<br />

and the second expression’s final operation results in a numeric.<br />

The following example shows multiple levels of nesting:<br />

WRITE 1+2*3-4*5,! // returns 25<br />

WRITE 1+(2*3)-4*5,! // returns 15<br />

WRITE 1+(2*(3-4))*5,! // returns -5<br />

WRITE 1+(((2*3)-4)*5),! // returns 11<br />

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

Tip:<br />

For all but the simplest <strong>ObjectScript</strong> expressions, it is good practice to fully parenthesize expressions. This is to<br />

eliminate any ambiguity about the order of evaluation and to also eliminate any future questions about the original<br />

intention of the code.<br />

For example, because the “&&” operator, like all operators, is subject to left-to-right precedence, the final statement in<br />

the following code fragment evaluates to 0:<br />

Set x = 3<br />

Set y = 2<br />

If x && y = 2 {<br />

Write "True",! } Else {<br />

Write "False",! }<br />

This is because the evaluation occurs as follows:<br />

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

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

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

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

<strong>Using</strong> <strong>Caché</strong> <strong>ObjectScript</strong> 33

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!