位置:首頁 > 腳本語言 > Lua教學 > Lua運算符優先級

Lua運算符優先級

運算符優先級來確定條件的表達式中的分組。這會影響一個表達式如何計算。某些運算符的優先級高於其他。

例子

試試下麵的例子就明白了在Lua編程語言的運算符所有的優先級:

a = 20
b = 10
c = 15
d = 5

e = (a + b) * c / d;-- ( 30 * 15 ) / 5
print("Value of (a + b) * c / d is   :",e )

e = ((a + b) * c) / d; -- (30 * 15 ) / 5
print("Value of ((a + b) * c) / d is :",e )

e = (a + b) * (c / d);-- (30) * (15/5)
print("Value of (a + b) * (c / d) is :",e )

e = a + (b * c) / d;  -- 20 + (150/5)
print("Value of a + (b * c) / d is   :",e )

當建立並執行上麵的程序它會產生以下結果:

Value of (a + b) * c / d is   :	90
Value of ((a + b) * c) / d is :	90
Value of (a + b) * (c / d) is :	90
Value of a + (b * c) / d is   :	50