下表显示了Lua语言支持的所有算术运算符。 假设变量A=10
,变量B=20
,然后 -
运算符 | 描述 | 示例 |
---|---|---|
+ |
相加两个操作数 | A + B = 30 |
- |
从第一个减去第二个操作数 | A - B = -10 |
* |
将两个操作数相乘 | A * B = 200 |
/ |
用除分子除以分子 | B / A = 2 |
% |
模数运算符,整数除法后的余数 | B % A = 0 |
^ |
指数运算符取指数幂值 | A^2 = 100 |
- |
一元,充当否定 | -A = -10 |
示例
尝试以下示例来了解Lua编程语言中可用的所有算术运算符 -
a = 21
b = 10
c = a + b
print("Line 1 - Value of c is ", c )
c = a - b
print("Line 2 - Value of c is ", c )
c = a * b
print("Line 3 - Value of c is ", c )
c = a / b
print("Line 4 - Value of c is ", c )
c = a % b
print("Line 5 - Value of c is ", c )
c = a^2
print("Line 6 - Value of c is ", c )
c = -a
print("Line 7 - Value of c is ", c )
执行上述程序时,会产生以下结果 -
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2.1
Line 5 - Value of c is 1
Line 6 - Value of c is 441
Line 7 - Value of c is -21