Script Operators
Following operators can be used in expressions.
Operator | Description |
---|---|
() Parenthesis. |
These are used to group operations, so they will be executed in the right order. "a = b * (c + d)" will first add c and d, then multiply with b and then assign a with the result. Without the parenthesis, since * has a higher precedence than +, b and c had first been multiplied and then added with d. |
[] Brackets. | These are used at the end of a variable to indicate an indexing of the object variable. An index can be a constant, or a local variable. The start index in an array is always 0. SENSORA[4] means fifth object of SENSORA. In this example, it means sensors number 4. SENSORA[st] means the object in array that the local variable st contains. If st contains 3, this means the fourth object. |
. Dot. | This is used to separate fields. SENSORA[5].FLAG means a flag in sensor 5. |
! + - Unary operators. | These unary operators can be used before an operand to change the return value. !SENSORA[5].FLAG returns 0 if sensor 5 flag is on and returns 1 if sensor 5 is not on. !var returns 0 if local variable var is non-zero and returns 1 if var is zero. “a = -b” assign the inverted (opposite sign) value of b to a. “a = -5” assign the constant -5 to a. “a = +b” assign b to a. The unary + has the same meaning as unary - but is also default. |
* / % Multiplicative operators. | * is multiplication, / is division and % is the reminder of a division. “a = 3 * b” assign 3 multiplied by b to a. “hour = seccnt / 3600;” assign the integer from the result of seccnt divided by 3600 (1 hour in seconds) to hour. “min = (seccnt % 3600) / 60;” assign the reminder of the previous operation (the number of seconds that was not covered in the hour, divided by 60 (1 minute in seconds) to min. “sec = (seccnt % 3600) % 60;” assign the rest to sec. |
+ - Additive operators | + is addition and - is subtraction. “a = b + c - 3;” assign b plus c minus 3 to a. |
< <= > >= Comparison. | < means less than, <= means less or equal to, > means greater than and >= means greater or equal to. “a = b <= c;” assign the result of the Comparison b less or equal to c to a. If b is less or equal to c, then a get the value 1, else a get the value 0. |
== != Comparison (equal). | == means equal to and != means not equal to. “a = b != c;” assign the result of the Comparison b not equal to c to a. If b is not equal to c, then a get the value 1, else a get the value 0. |
& ^ | Bitwise operators. | & is logical and between all bits in two operators, | is logical or between all bits in two operators and ^ is logical exclusive or between all bits in two operators “a = b & 0xff” assigns the lower byte in b to a. |
&& || Logical test. | && is a logical and between two operators and || is a logical or between two operators || “a = b && c || d;” assign the result of the logical test if b and c is true or if d is true. If both b and c are true (non-zero) or if d is true (non-zero), then a get the value 1, else a get the value 0. |
= Assign. | This operator assigns the result of the expression to the right to the variable to the left. |
Below follows a precedence list of the operators. The top row has highest precedence (priority). Operators at the same level are executed according to the associativity order.
Operator | Description |
---|---|
() [] . | Left to right. |
! + - | Right to left |
* / % | Left to right |
+ - | Left to right |
< <= > >= == != | Left to right |
& ^ | | Left to right |
& & | Left to right |
|| | Left to right |
= | Right to left |