2.8 Operators and Expressions
In the following example, the variable margin is added to the number value 500, and the resulting value is divided by 2. Thus, the calculated value is assigned to a variable called “height.”
var height, margin=10
height=(500+margin)/2
print height
Through the print statement, it is possible to check that 255, which is the result of the expression, is assigned to “height.”
In this way, an expression can be created by concatenating operands, which are values or variables, using various operators, and the result can be assigned to a variable or be used as a parameter of a statement.
What operation will be performed first if the addition sign and multiplication sign are used without grouping, as shown below? Multiplication and division will be performed first before addition and subtraction because there is an operation order in which operators are applied, which is called “operator precedence.” Because the operator precedence of multiplication is higher than that of addition, multiplication will be performed first even though the multiplication sign is located at a later place.
print 10+10*2
Strings will be concatenated when the (+) operator is used for them.
var name="axis1", type="rotational"
print name + ":" + type
The operators supported by HRScript are as follows. The higher it is, the higher the operator precedence. (In other words, operators with higher operator precedence will be executed first.)
Operator
Meaning
Example
( )
Grouping
(10+10)*2 ; 40
[ ]
Accessing array elements
arr[3]
**
Exponentiation
10**3 ; 1000
+x, -x
Sign
-300
*, /, mod
Multiplication, division, remainder
300/3 ; 100, 8 mod 3 ; 2
+, -
Addition, subtraction
300-100 ; 200
~
Bitwise NOT
~0b11010010
; 0b11111111111111111111111100101101
&
^
|
<<
>>
Bitwise AND
Bitwise XOR
Bitwise OR
Shift left
Shift right (sign maintained)
0b11010010 & 0b11110000 ; 0xd0
0b11010010 ^ 0b11110000 ; 0x22
0b11010010 | 0b11110000 ; 0xf2
0b11010010 << 2 ; 0b1101001000
0b11010010 >> 2 ; 0b00110100
<, <=, >, >=,
!=, ==
Comparison operation
(!=) means different, (==) means equal.
30 <= 29 ; false
response != "ok"
not x
Logical operation NOT
not error_state
and
or
Logical operation AND
Logical operation OR
height>100 and invert==false
timeout or work_count>3
When operands are number or Boolean values, the result of comparison and logical operations is a Boolean data type, and the result type of other operations is a number data type.
In cases where an operand of Boolean type is used without a comparison operator, it means whether its value is equal to true. For example, the two lines below have the same meaning.
var result= timeout
var result= (timeout==true)
Operators that can be used for strings are addition (+), comparison (!=, ==), and assignment (=). String addition makes it possible to concatenate operand strings, as previously shown.
A comparison operation determines whether a string is different or equal.
var response="ok"
print response=="ok"
print response=="ng"
Sometimes, the data type of an operand may change automatically during the process of operation.
When a number is used as an operand of a logical operator, the result will be regarded as false if it is 0 and true if it is not.
var count_a=1, count_b=0, height=100
print count_a and height>99
print count_b and height>99
“bitwise NOT” and “shift left/right” are calculated on a 32-bit length basis.
Last updated
Was this helpful?