IBM Tivoli Monitoring, Version 6.2.2 Fix Pack 2

Operators and expressions

Expressions perform specific actions, based on an operator, with one or two operands. An operand can be a constant, a variable or a function result. Operators are arithmetic, logical, and relational. As with C, some operators vary in functionality according to the data type of the operands specified in the expression.

Arithmetic operators ( +, -, *, /, **, % )
Arithmetic operators perform mathematical operations such as addition and subtraction with operands. There are two types of mathematical operators: unary and binary. Unary operators perform an action with a single operand. Binary operators perform actions with two operands. In a complex expression, (two or more operands) the order of evaluation depends on precedence rules.
Unary arithmetic operators
Unary operators are arithmetic operators that perform an action on a single operand. The script language recognizes the unary operator negative (-).

The negative unary operator reverses the sign of an expression from positive to negative or vice-versa. The net effect is that of multiplying the number by -1. Example:

a = -10;

The Prefix operators increment or decrement the variable prior to dereferencing the object, while the Postfix operators increment or decrement the variable after referencing it. Example:

A=1; 
B = a++; // b will equal 1, a will equal 2; 
A = 1; 
B = ++a; // b will equal 2, a will equal 2; 
A= 1; 
B = a--; // b will equal 1, a will equal 0;
Binary arithmetic operators
Insert a space before and after an arithmetic operator. The binary arithmetic operators that are supported are listed below.
Table 1. Binary Arithmetic Operators
Symbol Operation Example Description
+ Addition a + b Add the two operands
- Subtraction a - b Subtract the second operand from the first operand
* Multiplication a * b Multiply the two operands
/ Division a / b Divide the first operand by the second operand
** Power a ** b Raise the first operand by the power of the second operand
%

Modulo

a % b

Divide the first operand by the second operand and yield the remainder portion
Operator precedence
Expressions are normally evaluated left to right. Complex expressions are evaluated one at a time. The order in which the expressions are evaluated is determined by the precedence of the operators used. The standard C ordering is followed.
  1. negation (-) unary
  2. power
  3. multiplication, division and modulo
  4. addition and subtraction

If an expression contains two or more operators with the same precedence, the operator to the left is evaluated first. For example, 10 / 2 * 5 will be evaluated as (10 / 2) and the result multiplied by 5.

When a lower precedence operation should be processed first, it should be enclosed within parentheses. For example, 30 / 2 + 8. This is normally evaluated as 30 divided by 2 then 8 added to the result. If you want to divide by 2 + 8, it should be written as 30 / (2 + 8).

Parentheses can be nested within expressions. Innermost parenthetical expressions are evaluated first.

Assignment Operator (= )
Use the assignment operator (=) to copy a constant, literal, variable expression result, or function result to a variable. The script language does not support multiple assignments in a single statement (such as a=b=c=0). String lengths are defined based on the size of the string assigned to the variable and can change dynamically at runtime.
Logical Operators (AND, OR)
Logical operators allow the combining of more than one relational test in one comparison. Logical operators return a TRUE (1) or FALSE (0) value. Logical operators have a lower precedence than arithmetic operators.
Table 2. Logical Operators
Symbol Operation Example Description
AND &&

AND

Expr1 $$ expr2

True if both expr1 and expr2 are true.

OR ||

OR

Expr1 OR expr2

True if either expr1 or expr2 are true.

Relational Operators
Relational operators are as follows:
Table 3. Relational Operators
Symbol Operation Example Description
< Less than a < b True if a is less than b.
> Greater than a GT b True if a is greater than b.
== Equal a == b True if a is equal to b.
!= Not equal a NE b True if a is not equal to b.
<= Less than or equal a <= b True if a is less than or equal to b.
>= Greater than or equal a GE b True if a is greater than or equal to b.

Comparisons must be made on like data types—string to string, numbers to numbers. If the data types are different, a run time error will be raised.

String comparisons
Two strings are considered equal if they match character for character and are of the same length. Strings are compared character by character, left to right until all characters have been matched or one of the strings has been exhausted. If the end of one string is encountered before the end of the corresponding string, the strings are considered to be of unequal length and result in an unequal condition.

If any character in a string does not match the corresponding character in the other string, the comparison stops and the strings are considered not equal. Strings are case-sensitive. Examples:

Str1 = "abcdefg"; 
Str2 = "abcdefg"; 
Str3 = "abcdef"; 
Str4 = "ABCDEFG"; 
If (str1 == str2)... results in an equal/true conditions 
If (str1 == str3).... Results in a not equal condition 
because str3 is shorter than str1 
If (str1 == str4) ... Results in a not equal condition 
because the strings are of different case;
String Concatenation Operator (+)
The Plus(+) operator, when applied to strings, results in the concatenation of the two strings. Example:
Str1 = "abc"; 
Str2 = "def"; 
Str3 = str1 + str2; results in "abcdef"


Feedback