Expression examples
Examples of combinations to use for creating an expression:
Numeric literals in expressions
Name | Description | Example |
Integers | Numbers 0-9 specified in base-10 format. Leading zeros are not supported. 0 is supported, 01 is not supported. | 123 |
Floating point | Numbers that begin with zero or more base-10 digits, then the period character as decimal separator, followed by one or more base-10 digits. Leading zeros are not supported to the left of the decimal point, unless it is just a single zero immediately preceding the decimal point. Examples: 1.23 .23 0.23 | 1.23, .23, and 123.45 |
Exponential | Numbers that begin with an integer or floating point number, followed by the character 'e' (or 'E') and an integer. A negation operator '-' is supported before the exponent; other operators are not. | 6.02e23 means 6.02 * 1023 1.66e-27 means 1.66 * 10-27 |
String values (literal or tags) in expressions
Name | Description | Example: If tag1 = "SOME" and tag2 + "THING", then: |
String | A string literal begins with a double-quote character and ends with a double-quote character. Anything between the two double-quotes is part of the string literal. You can use strings as operands with the plus (+) operator and with relational operators. The + operator joins string operands. String tags and string literals behave the same way. Keep the following in mind when using string tags with the relational operators:
| tag1+tag2 returns "SOMETHING" |
Tags in expressions
Name | Description | Example: In a controller named Packing, if tag1 = 5 and tag2 = 7, then: |
Tag | A tag can stand alone as an expression or be included as part of an expression that is made up of other components. An expression that is a tag name returns the value of the tag. Tags begin with either "::" or "\". Use the format ::ControllerName.TagName. You can type the tag name or open the Tag Browser and select a tag. | ::Packing.tag1 returns 5::Packing.tag1 + 25 returns 30::Packing.tag1 % ::Packing.tag2 returns 5::Packing.tag1>::Packing.tag2 returns 0 (false)(::Packing.tag1<::Packing.tag2) &&(::Packing.tag1==5) returns 1 (both statements are true) |
Numeric operators in expressions
IMPORTANT:
- Using strings in numeric operations is not recommended. The exception is the + operator, which is used for string concatenation.
- Using LINT tags in expressions, write commands, or increment commands is not recommended. This is because the values may lose resolution as a result of performing the expression calculations.
- Ensure that any tag value you use as a divisor cannot at some point have a value of zero. Expressions that attempt to divide a number by zero produce an error at runtime.
Symbol (Operation) | Description | Example: If tag1 = 5 and tag2 = 7, then: |
+ (Addition) | Returns the sum of the values. SINT, INT, DINT, LINT, and REAL tags can be used in any order. All values are converted to double-precision floating point first, and then the operation is performed. If any operand is a string, it is concatenated. | tag1 + tag2 returns a value of 12MyDINT + "SomeString" returns a value of 5SomeString, if MyDINT = 5 |
- (Subtraction) | Returns the difference of the two values. SINT, INT, DINT, LINT, and REAL tags can be used in any order. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 - tag2 returns a value of -2 |
- (Negation) | Inverts the sign of the operand, making a positive number into a negative number or a negative number into a positive number. | -tag2 returns -7 |
* (Multiplication) | Returns the product of two values. SINT, INT, DINT, LINT, and REAL tags can be used in any order. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 * tag2 returns a value of 35 |
/ (Division) | Returns the quotient of the two values. SINT, INT, DINT, LINT, and REAL tags can be used in any order. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 / tag2 returns a value of 0.71 |
% (Modulus) | Returns the remainder of one number divided by another. SINT, INT, DINT, LINT, and REAL tags can be used in any order. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 % tag2 returns a value of 5. |
Math Function in expressions
Function | Description | Example |
ABS, Abs, or abs | Returns the absolute value of the expression. | ABS(-1.23) returns 1.23.ABS(1.23) returns 1.23. |
ARCCOS, arccos, ACOS, or acos | Returns the arc cosine of the expression in radians. | ACOS(-1.0) returns 3.14159. |
ARCSIN, arcsin, ASIN, or asin | Returns the arc sine of the expression in radians. | ARCSIN(1) returns 1.570796. |
ARCTAN, arctan, ATAN, or atan | Returns the arc tan of the expression in radians. | ATAN(-45.01) returns -1.54858. |
COS, Cos, or cos | Returns the cosine of the expression in radians. | COS(14.78) returns -0.599465. |
LOG, Log, or log | Returns the natural log of the expression. | LOG(2) returns 0.69314718 |
LOG10, Log10, or log10 | Returns the base-10 log of the parameter. | LOG10(100) returns 2. |
SIN, Sin, or sin | Returns the sine of the expression in radians. | SIN(45.175643) returns 0.929607. |
SQRT, Sqrt, or sqrt | Returns the square root of an expression. | SQRT(144) returns 12. |
TAN, Tan, or tan | Returns the tangent of the expression in radians. | TAN(1) returns 1.5574077246549023. |
TRUNC, Trunc, or trunc | Returns the value of the parameter with any digits to the right of the decimal point removed. | TRUNC(10.8282) returns 10. |
POW, pow | Returns the result of raising the first value to the power of the second value. All values are converted to double-precision floating point first, and then the operation is performed. | POW(10,3) returns 1000. |
String functions in expressions
Function | Description | Example |
TOLOWER, ToLower, or tolower | Converts any uppercase characters to lower lowercase. Converts a numeric to a string containing that number. | TOLOWER("New York") returns "new york".This expression converts a numeric to a string containing that number: TOLOWER(32) + TOLOWER(34) returns "3234". |
TOUPPER, ToUpper, or toupper | Converts any lowercase characters to uppercase. | TOUPPER("hello") returns "HELLO". |
Conditional statements in expressions
IMPORTANT:
Keep the following in mind when using conditional statements:
- The condition portion of the statement can be any numeric or Boolean expression.
- If the condition is a numeric expression, including a tag, zero is treated as false and any non-zero value is treated as true.
- Using a string operand (string Literals, string properties, string tags, or the result of a String Operation) as the condition of a conditional statement is not supported.
- All parts of a conditional statement are required.
The value if true and value if false portions of the conditional statement can be any valid expression, including another conditional statement.
Function | Description | Example: If tag1 = 5 and tag2 = 7, then: |
IIF, Iif, iif | Evaluates with the same rules as numeric operands. If the condition evaluates to 0 (false), the second value is used. If the condition evaluates to non-zero (true), the first value is used. Use this format: IIF(condition, value if true, value if false) | IIF( tag1 > tag2, tag1, tag2 ) returns 7 (the value of tag2) because tag1 is not greater than tag2 |
?: | The following requirements are specific to the ?: statement:
Use this format: condition ? value if true : value if false | (tag1 > tag2) ? tag1 : tag2 returns 7 (the value of tag2) because tag1 is not greater than tag2 |
Relational operators in expressions
Symbol (Function) | Description | Example: If tag1 = 5 and tag2 = 7, then: |
== (Equal to) | Compares the two values and returns a 1 (true) if they are equal. Returns a 0 (false) if they are not equal. Performs a case-sensitive string comparison. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 == tag2 is false, so the expression returns 0 |
!= (Not equal to) | Compares the two values and returns a 1 (true) if they are not equal. Returns a 0 (false) if they are equal. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 != tag2 is true, so the expression returns 1 |
< (Less than) | Compares two values and returns a 1 (true) if the value on the left is smaller than the value on the right. Returns a 0 (false) if not. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 < tag2 is true, so the expression returns 1 |
> (Greater than) | Compares two values and returns a 1 (true) if the value on the left is larger than the value on the right. Returns a 0 (false) if not. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 > tag2 is false, so the expression returns 0 |
<= (Less than or equal to) | Compares two values and returns a 1 (true) if the value on the left is smaller or the same as the value on the right. Returns a 0 (false) if not. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 <= tag2 is true, so the expression returns 1 |
>= (Greater than or equal to) | Compares two values and returns a 1 (true) if the value on the left is larger or the same as the value on the right. Returns a 0 (false) if not. All values are converted to double-precision floating point first, and then the operation is performed. | tag1 >= tag2 is false, so the expression returns 0 |
Logical operators in expressions
Symbol (Function) | Description | Example: If tag1 = 5 and tag2 = 7, then: |
&& (Logical AND) | Returns a value of 1 if the statements to the right and to the left of the operator are both true (non-zero). | (tag1 < tag2) && (tag1 == 5)r eturns a 1 because both statements are non-zero (true)tag1 && tag2 r eturns a 1 because both tag1 and tag2 are non-zero (true) |
ll (Logical OR) | Returns a value of 1 if either the statement to the left or to the right of the operator is true (non-zero). | (tag1 > tag2) ll (tag1 == 5)r eturns a value of 1 because tag1 == 5 is true |
! (Logical NOT) | Inverts the Boolean value of an expression. Returns true if the expression is false and returns false if the expression is true. If the expression (a>b) evaluates to true, then !(a>b) evaluates to false. | !(tag1 < tag2)
The expression tag1 < tag2 evaluates to true and returns a value of 1, but the NOT operator reverses the logical value, and 0 is returned. |
Bitwise operators in expressions
Symbol (Function) | Description | Example: If tag1 = 5 (binary 0000 0000 0000 0101) and tag2 = 3 (binary 0000 0000 0000 0011), then: |
& (Bitwise AND) | Returns an integer with a bit set to 1 if both of the corresponding bits in the original numbers are 1. Otherwise, the resulting bit is 0. | tag1 & tag2 returns 1 (binary 0000 0000 0000 0001) |
| (Bitwise inclusive OR) | Returns an integer with a bit set to 1 if either or both of the corresponding bits in the original numbers are 1. If both bits are 0, the resulting bit is 0. | tag1 | tag2 returns 7 (binary 0000 0000 0000 0111) |
^ (Bitwise exclusive XOR) | Returns an integer with a bit set to 1 if either of the corresponding bits in the original numbers is 1. If both bits are 1 or both are 0, the resulting bit is 0. | tag1 ^ tag2 returns 6 (binary 0000 0000 0000 0110) |
>> (Right Shift) | Shifts the bits within the left operand by the amount specified in the right operand. The bit on the right disappears. Either a 0 or a 1 is shifted in on the left depending on whether the integer is signed or unsigned. With unsigned integers, 0 is always shifted in on the left. With signed integers, a 0 is shifted in when the number is positive (that is, the leftmost bit--the sign bit--is 0), and a 1 is shifted in when the number is negative (that is, the leftmost bit--the sign bit--is 1). In other words, with signed integers, the sign of the number is always maintained. | tag1 >> 1 returns 2 (binary 0000 0000 0000 0010) |
<< (Left Shift) | Shifts the bits within the left operand by the amount specified in the right operand. The bit on the left disappears and a 0 is shifted in on the right. If the left bit is a 1, an overflow occurs, and an error message appears. To prevent this, use the bitwise AND (&&) operator in an expression. For example, (dev << 1) && 65535, where 65535 is 1111 1111 1111 1111 in binary form. Where dev is a tag name whose value is being shifted left. | tag1 << 1 returns 10 (binary 0000 0000 0000 1010) |
~ (Complement) | Gives the ones complement of a number. For example, use this operator to reverse every bit within the number so that every 1 bit becomes a 0 and vice versa. | ~ tag1 r eturns -6 (binary 1111 1111 1111 1010) |
Properties of graphic elements in expressions
IMPORTANT:
- Avoid setting up circular references using graphic element property bindings because this can result in unpredictable values or a runtime error.An example of a circular reference would be having three elements on the screen such that, for example, Element1.Opacity is bound to Element2.Opacity, Element2.Opacity is bound to Element3.Opacity, and Element3.Opacity is bound to Element1.Opacity.
- A numeric display’s .Value property is treated as a string when using the “+” operator, causing a concatenation to occur instead of an addition. To force an addition of .Value properties, first multiply .Value by 1.For example, (NumericDisplay_001.Value*1) + (NumericDisplay_002.Value*1) will perform an addition of the .Value properties. However, NumericDisplay_001.Value + NumericDisplay_002.Value will perform a concatenation of the .Value properties.
Name | Description | Example |
Property name | Property names refer to components of the current application. This differs from tags, which refer to data items in a controller. The graphic elements must all be on the same screen, popup, or System Banner . You cannot use an alias property in an expression. Use the syntax: ElementName.PropertyName An expression can contain:
| MyElement.X + MyElement.Y / MyElement.Opacity MyElement.X + ::MyController.MyTag + MyCustomProperty |
Punctuators in expressions
Symbol | Description | Example |
( ) | Opening and closing parentheses. Use parentheses around any subexpression to specify the order of operations. Subexpressions inside parentheses are evaluated prior to any operators outside the parentheses being applied. | 3 + 2 * 6 returns 15 (3 + 2) * 6 returns 30 |
Provide Feedback