Precedence: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
No edit summary
>JulienDethurens
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
__TOC__
== Introduction ==
== Introduction ==


Operators take <b>precedence</b> over another in a certain order in Lua.  This can affect the outcome of your formulae.
[[Operators]] take precedence over another in a certain order in Lua.  This can affect the outcome of your formulae.


== Discussion ==
== Discussion ==
Here is what the Lua 5.1 Reference Manual says about operator precedence:
<blockquote cite="http://www.lua.org/manual/5.1/manual.html#2.5.6">
Operator precedence in Lua follows the table below,
from lower to higher priority:


<pre>
<pre>
^
    or
not  - (unary)
    and
*  /
    &lt;    &gt;    &lt;=   &gt;=   ~=   ==
+  -
    ..
..
    +    -
<  >  <= >= ~= ==
    *    /    %
and
    not  #    - (unary)
or
    ^
</pre>
</pre>


For example:<br>
As usual, you can use parentheses to change the precedences of an expression.
print(-3^2)<br>
The concatenation ('..') and exponentiation ('^')
Will result in:<br>
operators are right associative.
-9<br>
All other binary operators are left associative.
Because 3 is raised (^) to the second power <b>first</b> and <b>then</b> the sign of 9 is changed from positive to negative.
</blockquote>
 
For example:
 
{{code|=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:
{{code|=print((-3)^2)}}
Will result in: 9
 
Here's another example:
 
{{code|=print(3 .. 2^2)}}
 
Will result in: 34
Notice that 2^2 is evaluated (2^2=4) before .. , giving us the value of 34.
 


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


Here's another example:<br>
{{code|=print (0 < 1 and 2 <= 7)}}
print(3 .. 2^2)<br>
Will result in:<br>
34<br>
Notice that 2^2 is evaluated (2^2=4) before .. , giving us the value of 34.<br>


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


== See Also ==
Notice that the inequalities are evaluated before the '''and'''.


[http://www.lua.org/pil/3.5.html 3.5 - Precedence]
==See Also==
*[http://www.lua.org/manual/5.1/manual.html#2.5.6 Lua 5.1 Reference Manual: Precedence]
*[http://www.lua.org/pil/3.5.html Programming in Lua: Precedence]

Latest revision as of 01:59, 28 March 2012

Introduction

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

Discussion

Here is what the Lua 5.1 Reference Manual says about operator precedence:

Operator precedence in Lua follows the table below, from lower to higher priority:

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

As usual, you can use parentheses to change the precedences of an expression. The concatenation ('..') and exponentiation ('^') operators are right associative. All other binary operators are left associative.

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