运算符是一个符号,告诉解释器执行特定的数学或逻辑操作。 Lua语言具有丰富的内置运算符,并提供以下类型的运算符-

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 其它运算符

本教程将逐一说明算术,关系,逻辑和其他各种运算符。

算术运算符

下表显示了Lua语言支持的所有算术运算符。假设变量 A=10,变量 B=20,然后-

Operator
Remark Example
+ A + B=30
- A-B=-10
* A * B=200
/ B/A=2
模运算符和整数除后的余数 B%A=0
^ 指数运算符取指数 A ^ 2=100
- 一元运算符,充当否定符 -A=-10

尝试以下示例以了解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语言支持的所有关系运算符。假设变量 A =10,变量 B=20,然后-

Operator Remark Example
== 相等 (A == B) is not true.
~= 如果值不相等,则条件为真。 (A ~= B) is true.
> 大于 (A > B) is not true.
< 小于 (A < B) is true.
>= 大于或等于 (A >= B) is not true.
<= 小于或等于 (A <= B) is true.

尝试以下示例以了解Lua编程语言中可用的所有关系运算符-

a=21
b=10

if( a == b )
then
   print("Line 1 - a is equal to b" )
else
   print("Line 1 - a is not equal to b" )
end

if( a ~= b )
then
   print("Line 2 - a is not equal to b" )
else
   print("Line 2 - a is equal to b" )
end

if ( a < b )
then
   print("Line 3 - a is less than b" )
else
   print("Line 3 - a is not less than b" )
end

if ( a > b ) 
then
   print("Line 4 - a is greater than b" )
else
   print("Line 5 - a is not greater than b" )
end

-- Lets change value of a and b
a=5
b=20

if ( a <= b ) 
then
   print("Line 5 - a is either less than or equal to  b" )
end

if ( b >= a ) 
then
   print("Line 6 - b is either greater than  or equal to b" )
end

当您构建并执行上述程序时,它将产生以下输出-

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not less than b
Line 4 - a is greater than b
Line 5 - a is either less than or equal to  b
Line 6 - b is either greater than  or equal to b

逻辑运算符

下表显示了Lua语言支持的所有逻辑运算符。假设变量 A=true,变量 B=false,则-

Operator
Remark
Example
and 称为逻辑AND运算符。如果两个操作数都不为零,则条件为true。 (A和B)为假。
or 称为逻辑或运算符。如果两个操作数中的任何一个都不为零,则条件为true。 (A或B)为真。
not 称为逻辑非运算符。用于反转其操作数的逻辑状态。如果条件为真,则逻辑非运算符将为假。 !(A和B)为真。

请尝试以下示例,以了解Lua编程语言中可用的所有逻辑运算符-

a=5
b=20

if ( a and b )
then
   print("Line 1 - Condition is true" )
end

if ( a or b )
then
   print("Line 2 - Condition is true" )
end

--lets change the value ofa and b
a=0
b=10

if ( a and b )
then
   print("Line 3 - Condition is true" )
else
   print("Line 3 - Condition is not true" )
end

if ( not( a and b) )
then
   print("Line 4 - Condition is true" )
else
   print("Line 3 - Condition is not true" )
end

当您构建并执行上述程序时,它将产生以下输出-

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is true
Line 3 - Condition is not true

其它运算符

Lua语言支持的其他运算符包括 concatenation 和 length 。

Operator
Remark
Example
.. 连接两个字符串。 a..b,其中a为" Hello",b为" World",将返回" Hello World"。
# 返回字符串或表格长度的一元运算符。 #"hello"将返回5

尝试以下示例以了解Lua编程语言中可用的其他运算符-

a = "Hello "
b = "World"

print("Concatenation of string a with b is ", a..b )

print("Length of b is ",#b )

print("Length of b is ",#"Test" )

当您构建并执行上述程序时,它将产生以下结果-

Concatenation of string a with b is 	Hello World
Length of b is 	5
Length of b is 	4

运算符优先级

在这里,优先级最高的运算符出现在表格的顶部,而优先级最低的运算符出现在表格的底部。

Category Operator Associativity
Unary not # - Right to left
Concatenation .. Right to left
Multiplicative */% Left to right
Additive + - Left to right
Relational < > <= >= == ~=  Left to right
Equality == ~= Left to right
Logical AND and Left to right
Logical OR or Left to right

尝试以下示例以了解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

参考链接

https://www.learnfk.com/lua/lua-operators.html