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
lua_operators.htm