Previous ION Script User's Guide: Variables, Expressions & Operators Next

Operators

Operators are used to perform comparisons and mathematical operations on the terms of an expression. Operators are used between values or expressions in the general form value1 OP value2. Some operators only accept values of a certain type. If the value for a variable is not a type supported by the operation, the operation will fail.

Mathematical Operators

ION Script's mathematical operators are listed in the following table.

Table 3-3: Mathematical Operators 

Table 3-3: Mathematical Operators 
Operator
Description
Types Accepted
+
Addition: Add value1 to value2.
variables, numbers
-
Subtraction. Subtract value2 from value1
variables, numbers
/
Division. Divide value1 by value2
variables, numbers
*
Multiplication. Multiply value1 by value2
variables, numbers
MOD
Modulus. Returns the integer remainder of dividing value1 by value2
variables, numbers
^
Raise to the Power Of. Returns value1 raised to the power value2
variables, numbers

String Operators

ION Script's string operators are listed in the following table.

Table 3-4: String Operators 

Table 3-4: String Operators 
Operator
Description
Types Accepted
+
String concatenation. 'String1' + 'String2' creates 'String1String2'.
variables, numbers, strings
CONTAINS
String contains. value1 CONTAINS value2 returns true if value2 is found in value1, returns false otherwise.
variables, strings

Comparison Operators

ION Script's comparison operators are listed in the following table.

Table 3-5: Comparison Operators 

Table 3-5: Comparison Operators 
Operator
Description
Types Accepted
GT
Greater Than. TRUE if value1 is greater than value2
variables, numbers
GE
Greater Than or Equal To. TRUE if value1 is greater than or equal to value2
variables, numbers
LT
Less Than. TRUE if value1 is less than value2
variables, numbers
LE
Less Than or Equal To. TRUE if value1 is less than or equal to value2
variables, numbers
EQ
Equality. TRUE if value1 is equal to value2
variables, numbers, strings
NE
Not Equal To. TRUE if value1 is NOT equal to value2
variables, numbers, strings
ISTYPE
TRUE if the data type of the variable specified by value1 corresponds to value2, a string representing the variable type. value2 can be one of the following string literals: 'BOOL', 'STR', 'INT', 'DOUBLE', or 'UNDEF'.
variables, numbers, strings

Logical Operators

ION Script's logical operators are listed in the following table. Note that values used with logical operators can be values, variables, or expressions, but they must evaluate to a boolean value.

Table 3-6: Logical Operators 

Table 3-6: Logical Operators 
Operator
Description
Types Accepted
AND
Logical AND. value1 AND value2 returns true if both value1 and value2 evaluate to true. Returns false otherwise.
Boolean value or variable, or expression that evaluates to a boolean
OR
Logical OR. value1 OR value2 returns true if either value1 or value2 evaluates to true. Returns false otherwise.
Boolean value or variable, or expression that evaluates to a boolean
NOT
Logical negation. NOT value returns true if value is false and returns false if value is true.
Boolean value or variable, or expression that evaluates to a boolean

The following are examples of valid expressions using the logical operators. For these examples, assume

$BOOL1 = true  
$BOOL2 = false  
$A = 1  
$B = 2

 

Table 3-7: Logical Operator Results 

Table 3-7: Logical Operator Results 
Type of operand
Expression
Result
Boolean variables
$BOOL1 AND $BOOL2
false
$BOOL1 OR $BOOL2
true
Boolean values
true AND false
false
true OR false
true
Expressions that evaluate to booleans
($A NE $B) AND ($A EQ 1)
true
($A EQ 5) OR ($B EQ 5)
false
NOT ($A EQ $B)
true
$BOOL1 AND ($A EQ 1)
true
  

Operator Precedence

The following table lists the ION Script operators in order of precedence. All operators are left associative, meaning that operators of equal precedence (such as + and -) are evaluated from left to right.

Table 3-8: Operator Precedence 

Table 3-8: Operator Precedence 
Precedence
Operators
Highest
Lowest
( )
- (negation)
^
NOT
*, /, MOD
+, -
GT, GE, LT, LE, CONTAINS, ISTYPE
AND
OR
EQ, NE

Grouping Expressions With Parentheses

Parentheses are used in ION Script to control the order of evaluation of expressions. Expressions that are contained inside parentheses are evaluated first by ION Script. When multiple sets of parentheses are nested within one another, the expression contained in the innermost set of parentheses is evaluated first.

For example, consider the following code:

<ION_IF EXPR="$Month EQ 'April' OR $Month EQ 'May'   
   AND $Temp EQ 70 ">  

This statement is equivalent to the following:

<ION_IF EXPR="$Month EQ 'April' OR   
   ($Month EQ 'May' AND $Temp EQ 70)">  

If your intent was to require $Temp to be 70, but allow the $Month to be April or May, you would need to override the precedence as follows:

<ION_IF EXPR="($Month EQ 'April' OR $Month EQ 'May') AND   
   $Temp EQ 70 ">  

 


Tip
It is good programming practice to use parentheses to control the order of evaluation, even when not technically necessary. This ensures that your expressions are being evaluated in the order you intended, and makes your code more readable.

Examples

The following table gives several examples of the order in which expressions are evaluated in ION Script:

Table 3-9: Expression Evaluation 

Table 3-9: Expression Evaluation 
Expression
Result
10 * 5 - 2
48
10 * (5 - 2)
30
3 + 4 * 2 ^ 2 / 2
11
(3 + (4 * 2) ^ 2 / 2)
35

  IDL Online Help (March 06, 2007)