Description
The order of operator evaluation is listed below, where higher in the list means higher precedence.
-
()
-
parentheses: grouping and evaluation precedence
integer val = a * (b + c);
-
[]
-
brackets: list constructor
list lst = [a, 2, "this", 0.01];
-
(type)
-
typecasting
string message = "The result is:" + (string)result;
-
!
~
++
--
-
logical NOT, bitwise NOT, increment, decrement
counter++;
-
*
/
%
-
multiply/dot product, divide, modulus/cross product
integer rollover = (count + 1) % 5;
-
-
-
subtraction, negation
integer one = 3 - 2;
-
+
-
addition, string concatenation
integer two = 1 + 1;
-
+
-
list concatenation
list myList = [1, 2, 3] + [4, 5];
-
<<
>>
-
arithmetic shift
integer eight = 4 << 1;
-
<
<=
>
>=
-
less than, less than or equal to, greater than, greater than or equal to
integer isFalse = (6 <= 4);
-
==
!=
-
comparison: equal, not equal
integer isFalse = ("this" == "that");
-
&
-
bitwise AND
integer zero = 4 & 2;
-
^
-
bitwise XOR
integer zero = 4 ^ 4;
-
|
-
bitwise OR
integer four = 4 | 4;
-
&&
||
-
logical AND, logical OR
integer isFalse = (FALSE && TRUE);
-
=
+=
-=
*=
/=
%=
-
assignment
integer four = 4;
Caveats
Conditional checks
Some conditional checks may need to be bracketed to prevent unexpected evaluation order. More specifically,
take extra care when using operators with lower precedence than equality ==
!=
. These include bitwise,
logical and assignment operators.
if (TRUE && FALSE == FALSE || TRUE)
{
llOwnerSay("TRUE");
}
else
{
llOwnerSay("FALSE");
}
// outputs "TRUE" because the equality operator was evaluated first
// i.e. TRUE && (FALSE == FALSE) || TRUE
if ((TRUE && FALSE) == (FALSE || TRUE)) // <== add some brackets this time
{
llOwnerSay("TRUE");
}
else
{
llOwnerSay("FALSE");
}
// outputs "FALSE"
No short-circuiting
Unlike most other languages that use the C-style &&
and ||
operators, both operands are always evaluated. For example,
if (TRUE || 1 / 0) llSay(PUBLIC_CHANNEL, "Aha!"); // throws Math Error