Precedence: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
No edit summary
 
>Mindraker
No edit summary
(No difference)

Revision as of 10:17, 18 October 2008

Introduction

Operators take precedence over another in a certain order in Lua. This can affect the outcome of your formulae.

Discussion

^
not  - (unary)
*   /
+   -
..
<   >   <=  >=  ~=  ==
and
or

For example:
print(-3^2)
Will result in:
-9
Because 3 is raised (^) to the second power first and then the sign of 9 is changed from positive to negative.

Notice that if we explicitly use parentheses, however, the outcome is different:
print((-3)^2)
Will result in:
9

Here's another example:
print(3 .. 2^2)
Will result in:
34
Notice that 2^2 is evaluated (2^2=4) before .. , giving us the value of 34.

Finally,
print (0 < 1 and 2 <= 7)
Will result in:
true
Notice that the inequalities are evaluated before the and.

See Also

3.5 - Precedence