Operators in TorqueScript behave very similarly to operators in real world math and other programming languages. You should recognize quite a few of these from math classes you took in school, but with small syntactical changes. The rest of this section will explain the syntax and show a brief example, but we will cover these in depth in later guides.
Arithmetic Operators
|
Operator
|
Name
|
Example
|
Explanation
|
|
multiplication
|
$a * $b
|
Multiply $a and $b.
|
/
|
division
|
$a / $b
|
Divide $a by $b.
|
%
|
modulo
|
$a % $b
|
Remainder of $a divided by $b.
|
+
|
addition
|
$a + $b
|
Add $a and $b.
|
-
|
subtraction
|
$a - $b
|
Subtract $b from $a.
|
++
|
auto-increment
(post-fix only)
|
$a++
|
Increment $a. Note: ++$a is illegal.
Note: the value of $a++ is that of the incremented variable: auto-increment is post-fix in syntax, but pre-increment in sematics (the variable is incremented, before the return value is calculated). This behavior is unlike that of C and C++.
|
- -
|
auto-decrement
(post-fix only)
|
$b--
|
Decrement $b. Note: --$b is illegal.
Note: the value of $a-- is that of the decremented variable: auto-decrement is post-fix in syntax, but pre-decrement in sematics (the variable is decremented, before the return value is calculated). This behavior is unlike that of C and C++.
|
Relations (Arithmetic, Logical, and String)
|
Operator
|
Name
|
Example
|
Explanation
|
<
|
Less than
|
$a < $b
|
1 if $a is less than % b (0 otherwise.)
|
>
|
More than
|
$a > $b
|
1 if $a is greater than % b (0 otherwise.)
|
<=
|
Less than or Equal to
|
$a <= $b
|
1 if $a is less than or equal to % b (0 otherwise.)
|
>=
|
More than or Equal to
|
$a >= $b
|
1 if $a is greater than or equal to % b (0 otherwise.)
|
==
|
Equal to
|
$a == $b
|
1 if $a is equal to % b (0 otherwise.)
|
!=
|
Not equal to
|
$a != $b
|
1 if $a is not equal to % b (0 otherwise.)
|
!
|
Logical NOT
|
!$a
|
1 if $a is 0 (0 otherwise.)
|
&&
|
Logical AND
|
$a && $b
|
1 if $a and $b are both non-zero (0 otherwise.)
|
||
|
Logical OR
|
$a || $b
|
1 if either $a or $b is non-zero (0 otherwise.)
|
$=
|
String equal to
|
$c $= $d
|
1 if $c equal to $d .
|
!$=
|
String not equal to
|
$c !$= $d
|
1 if $c not equal to $d.
|
Bitwise Operators
|
Operator
|
Name
|
Example
|
Explanation
|
~
|
Bitwise complement
|
~$a
|
flip bits 1 to 0 and 0 to 1. (i.e. ~10b == 01b)
|
&
|
Bitwise AND
|
$a & $b
|
composite of elements where bits in same position are 1. (i.e. 1b & 1b == 1b)
|
|
|
Bitwise OR
|
$a | $b
|
composite of elements where bits 1 in either of the two elements. (i.e. 100b & 001b == 101b)
|
^
|
Bitwise XOR
|
$a ^ $b
|
composite of elements where bits in same position are opposite. (i.e. 100b & 101b == 001b)
|
<<
|
Left Shift
|
$a << 3
|
element shifted left by 3 and padded with zeros. (i.e. 11b << 3d == 11000b)
|
>>
|
Right Shift
|
$a >> 3
|
element shifted right by 3 and padded with zeros. (i.e. 11010b >> 3d == 00011b)
|
Assignment and Assignment Operators
|
Operator
|
Name
|
Example
|
Explanation
|
=
|
Assignment
|
$a = $b;
|
Assign value of $b to $a.
Note: the value of an assignment is the value being assigned, so $a = $b = $c is legal.
|
op=
|
Assignment Operators
|
$a op= $b;
|
Equivalent to $a = $a op $b, where op can be any of:
/ % + - & | ^ << >>
|
String Operators
|
Operator
|
Name
|
Example
|
Explanation
|
@
|
String concatenation
|
$c @ $d
|
Concatenates strings $c and $d into a single string. Numeric literals/variables convert to strings.
|
NL
|
New Line
|
$c NL $d
|
Concatenates strings $c and $d into a single string separated by new-line.
Note: such a string can be decomposed with getRecord()
|
TAB
|
Tab
|
$c TAB $d
|
Concatenates strings $c and $d into a single string separated by tab.
Note: such a string can be decomposed with getField()
|
SPC
|
Space
|
$c SPC $d
|
Concatenates strings $c and $d into a single string separated by space.
Note: such a string can be decomposed with getWord()
|