Lua - 算术运算符
下表列出了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
lua_operators.htm