Operators

A list of all VTScada operators.

Spaces

You are advised to separate operators and operands with a space. For example:

X = A + B;

By doing so, you make your code easier to read and make syntax errors less likely. The exception to this recommendation is for operators that come before or after their operands, such as pre-increment and post-decrement. These should never be separated from their operands.

A = ++B  { Example valid only for script code, not expressions. };

Operator Priority in Statements

The order in which the operators are executed is significant. Consider the expression:

1 + 2 * 3

If operators were evaluated from left to right, the value of this expression would be 9, but in reality the value is 7. This is because VTScada, like all programming languages, assigns varying priorities to operators. Operators with higher priority are done first. Multiplication has a higher priority than addition, so in the previous example the correct result is 7.

If the addition were intended to be done before the multiplication, the expression could be written as:

(1 + 2) * 3

Parentheses ( ) have the highest priority of all operators and force the expression within them to be evaluated first.

Some operators have equivalent priorities, such as addition and subtraction. In these cases the evaluation proceeds from left to right.

If you have any doubt as to the order of execution within an expression, add parentheses to state (or control) the order explicitly. They cost nothing in terms of speed or memory requirements.

Operators and Invalid

Any expression that contains an Invalid will return Invalid unless PickValid() is used to substitute a valid value. There are two exceptions to this rule: AND (&&) and OR (||).

Boolean logic dictates that anything AND 0 is always a 0. Likewise, anything OR 1 must return 1.

Operators and Tag Expressions

All operators may be used in script code. Several are not available to, or not appropriate within, expressions written in tags and Idea Studio properties. For example:

  • The assignment operator, =, and any variation thereof such as add equals, cannot be used in a tag expression.
  • In a tag expression, square brackets are used to indicate tag names, and therefore do not work as array index operators.
  • The address and dereference operators are not meant to be used in tag expressions.
  • Pre- and Post- increment and decrement operators are not available for tag expressions.
  • Shift left and Shift right.

Operators, listed by priority

Operator Symbol Priority
Parentheses ( ) 1
X = 2 + 3 * 4; 
X = (2 + 3) * 4;

Controls the order of execution. In the first example, X will be 14. In the second, X is assigned 20.

Scope Resolution - local . 2
X = .A;

A may be a variable or module. If found in the local scope, it will be assigned to X, otherwise Invalid will be returned. Note that the dot operator will not work in applications created before version 11.2 unless the application property LocalScopeSyntax is set to 1.

Scope Resolution - not local \ 2
X = \A;

A may be a variable or module. VTScada will search through the scope tree starting with the local scope to find the closest variable or module with a matching name.

Index (Array or Dictionary) [ ] 3
X = A[2];

Follows the name of an array or dictionary variable, and specifies which element of the array or dictionary is to be used.

Segment/Offset @ 4

This returns a number which is the real mode address with the segment before the @ and the offset after the @. This value can be used in functions such as MemIn and MemOut.

Dereference
(pointer dereference)
* 5
A = *X;

The example shows the opposite of the example for the Address operation.

Address & 5
X = &A;

Returns a pointer to the operand. A pointer is the memory address where the operand's value is stored.
To retrieve the value from the memory address stored in X you must dereference the pointer.

Logical Not ! or ~ 5
X = !A;

The operand should be a Boolean TRUE or FALSE (1 OR 0). Returns 0 for a logical TRUE operand and 1 for a logical FALSE operand.

Pre-Increment ++ 5
A = 3;
						X = ++A;

Requires a single numeric operand. The value of the operand will be increased by one before it is used in the expression. If A initially had a value of 3 in the example given, then it would increase to 4 and that number would be assigned to X.

Post-Increment ++ 5
A = 3;
						X = A++;

Requires a single numeric operand. The value of the operand will be increased by one after it is used in the expression. If A initially had a value of 3 in the example given, then 3 would be assigned to X, following which A would increase to 4.

Pre-Decrement -- 5
A = 3;
X = --A;

Requires a single numeric operand. The value of the operand will be decreased by one before it is used in the expression. If A initially had a value of 3 in the example given, then it would decrease to 2 and that number would be assigned to X.

Post-Decrement -- 5
A = 3;
X = A--;

Requires a single numeric operand. The value of the operand will be decreased by one after it is used in the expression. If A initially had a value of 3 in the example given, then 3 would be assigned to X, following which A would decrease to 2.

Unary negation - 5
X = -A;

Requires a single numeric operand. Returns the negative of the value to the right without changing that value.

Division / 6
X = A / B;

Requires two numeric operands. Returns the result of the first divided by the second.

Modulus % 6
X = A % B;

Requires two numeric operands. Returns the remainder, being the amount left over after the maximum number of times that B can be wholly divided into A. For example, 8.5 % 3 returns 2.5
9 % 3 returns 0.

Multiplication * 6
X = A * B;

Requires two numeric operands. Returns the result of the first multiplied by the second.

Addition / Concatenation + 7
Subtraction - 7
X = A - B;

Returns the numeric result of subtracting B from A.
Operands must be numeric.

X = A + B;

Returns the sum of two numeric operands.
Or, concatenates two strings.

It is risky to use + for concatenation. If one operand can be interpreted as a number, it will not be cast to a string and the operation will fail. Use the Concat function instead.

Shift Left << 8
A = 4;
X = <<A;

Shift bits to the left. In binary, 4 is stored as 00000100. In this example, X will be assigned 00001000, or 8.

Shift Right >> 8
A = 4;
X = >>A;

Shift bits to the right. In binary, 4 is stored as 00000100. In this example, X will be assigned 00000010, or 2.

Greater Than > 9
X = A > B;

This operator returns TRUE (1) if the first argument is strictly greater than the second, otherwise it returns false (0).
If one argument is text and the other numeric, VTScada will attempt to cast to matching types.
If both arguments are text values, the operator returns true if a letter-by-letter comparison of the ASCII codes in the first argument is greater than the second. Thus "B" is greater than "A", but not greater than "a".

Greater Than or Equal To >= or => 9
X = A >= B;

The same as Greater Than, but will also return TRUE (1) if the two operands are equivalent. Does not do a test for strict equivalency. Text comparisons are case-sensitive.

Less Than < 9
X = A < B;

This operator returns TRUE (1) if the first argument is strictly less than the second, otherwise it returns false (0).
If one argument is text and the other numeric, VTScada will attempt to cast to matching types.
If both arguments are text values, the operator returns true if a letter-by-letter comparison of the ASCII codes in the first argument is lower than the second. Thus "a" is less than "b", but not less than "B".

Less Than or Equal To <= 9
X = A <= B;

The same as Less Than, but will also return TRUE (1) if the two operands are equivalent. Does not do a test for strict equivalency. Text comparisons are case-sensitive.

Equal To == 10
X = A == B;

Returns TRUE (1) if the two operands are equivalent.
Does not do a test for strict equivalency. VTScada will attempt to cast mismatched types to match.
Text comparisons are case sensitive. Use StrICmp for text comparisons that are not case sensitive.

Not Equal To != or <> or >< 10
X = A != B;

Returns TRUE (1) if the two operands are not equivalent.
Does not do a test for strict equivalency. VTScada will attempt to cast mismatched types to match. Text comparisons are case sensitive.

Exclusive Or ^ 11
X = 0 ^ 0; { returns 0 }
X = 1 ^ 0; { returns 1 }
X = 0 ^ 1; { returns 1 }
X = 1 ^ 1; { returns 0 }

This returns the 32-bit, bitwise exclusive OR of the two operands. If either operand (but not both) is TRUE (non-0), the result is TRUE (1). If both are TRUE or both FALSE (0), the result is FALSE.

AND (Logical And) && 12
X = A && B;

Both the & and the && operators(*see note 1) take two arguments and perform the logical AND function upon them. If both arguments are true (non-0), the operator returns a true value; otherwise, a false value (0) is returned. If either argument is a valid false, the function returns false regardless of whether the other argument is valid.

The AND operator performs a bitwise comparison (32-bit).

VTScada does not use short-circuit evaluation. Both parts of the condition will always be checked (and evaluated if required).

Note 1: VTScada supports a logical "&" operator but in practice it is not used, in order to avoid confusion with the C/C++ bitwise comparison operator.

Note 2: To perform bitwise comparisons, use the AND function.

OR (Logical Or) || 13
X = A || B;

The | and || operators(*see note 1) take two arguments and perform the logical OR function upon them. If either argument is true (non-0), the operator returns a true (1) value. If both arguments are false(0), the operator returns a false value(0).

If either argument is a valid true, the function returns true regardless of whether the other argument is valid. If one argument is false and the other invalid, the operator returns invalid.

The || operator performs a 32-bit, bitwise comparison.

VTScada does not use short-circuit evaluation. Both parts of the condition will always be checked (and evaluated if required).

Note 1: VTScada supports a logical "|" operator but in practice it is not used, in order to avoid confusion with the C/C++ bitwise comparison operator.

Note 2: To perform bitwise comparisons, use the OR function.

Ternary Operator (IfElse) C ? A : B 14
X = C ? A : B;

Equivalent to the IfElse function. Operands may be variables or expressions.

If the conditional, C, evaluates to TRUE, then expression A will be evaluated and returned. Otherwise expression B will be evaluated and returned. Note that the ? operator has a lower precedence than most other operators. You are advised to use parenthesis to control the order of evaluation if using If Else in a multi-part comparison. For example,

 A < B || C ? D : E

would be evaluated as

 ((A < B) || C) ? D : E

rather than

 (A < B) || (C ? D : E)

which is more likely what was intended.

Note that a deeply-nested set of IfElse operations can usually be replaced by simpler programming technique. Refer to the comment section in the function,IfElse

Add Equals += 15
X += A;

X and A must be numeric. Adds the value of A to the value of X.

Assignment = 15
X = A;

Assigns the value of the expression on the right side of the operator to the variable on the left.

Divide Equals /= 15
X /= A;

Requires two numeric operands. X is given the value of X / A.

Modulus Equals %= 15
X %= A;

Requires two numeric operands. X is given the remainder of X % A.

Multiply Equals *= 15
X *= A;

Requires two numeric operands. X is given the value of X * A.

Subtract Equals -= 15
X -= A;

X and A must be numeric. X is given the value X - A.