当前位置:网站首页>04 Lua 运算符
04 Lua 运算符
2022-04-23 16:13:00 【陈皮的JavaLib】
运算符
赋值运算符
赋值运算符=
可以将一个或者多个值赋值给一个或者多个变量。
name = "chenpi"
age = 18
a, b = 10, 20
print(name) -- chenpi
print(age) -- 18
print(a) -- 10
print(b) -- 20
而且还可以简单交互两个变量的值,如下
a, b = 10, 20
print(a) -- 10
print(b) -- 20
a, b = b, a
print(a) -- 20
print(b) -- 10
算数运算符
- 加法:+
- 减法:-
- 乘法:*
- 除法:/
- 取模:%
- 取负:-
- 求幂:^
print(10 + 20) -- 30
print(20 - 10) -- 10
print(2 * 5) -- 10
print(15 / 2) -- 7.5
print(15 % 2) -- 1
print(-20) -- -20
print(2^5) -- 32
比较运算符
- 大于:>
- 大于等于:>=
- 小于:<
- 小于等于:<=
- 等于:==
- 不等于:~=
print(10 > 20) -- false
print(20 >= 10) -- true
print(2 < 5) -- true
print(15 <= 2) -- false
print(15 == 12) -- false
print(12 ~= 13) -- true
关系运算符
- 并且:and
- 或者:or
- 非:not
对于 and 运算符,如果第一个表达式为真,并且第二个表达式的运算结果不是一个布尔型的值时,会输出这个值。
print(10 > 5 and 2 ~= 3) -- true
print(5 > 10 and 5) -- false
print(5 < 10 and 5) -- 5
print(5 < 10 and nil) -- nil
print(5 < 10 and "chenpi") -- chenpi
对于 or 运算符,如果第一个表达式为假,并且第二个表达式的运算结果不是一个布尔型的值时,会输出这个值。
print(10 > 5 or 2 ~= 2) -- true
print(5 > 10 or 10 > 2) -- true
print(5 < 2 or 5) -- 5
print(5 < 2 or nil) -- nil
print(5 < 2 or "chenpi") -- chenpi
not 取非,即将布尔值取反,在 Lua 中,只有 false 和 nil 代表 false,其他值都代表 true 。
print(not 5) -- false
print(not 0) -- false
print(not "chenpi") -- false
print(not nil) -- true
print(not true) -- false
print(not false) -- true
print(not (5 > 4 and 5 == 3)) -- true
print(not type) -- false
print(not print) -- false
连接符
Lua 中对字符串的连接使用符号..
,而不是像其他语言使用+
,加号在 Lua 中代表运算。
name = "Hello" .. " " .. "ChenPi"
print(name) -- Hello ChenPi
print("Lua " .. name .. "!") -- Lua Hello ChenPi!
age = 18
print(age .. " years old") -- 18 years old
b = true
print("boolean:" .. b) -- 不能连接boolean值,会报错 attempt to concatenate global 'b' (a boolean value)
print(address .. " area") -- 不能连接一个未定义的变量,会报错 attempt to concatenate global 'address' (a nil value)
print(nil .. " area") -- 不能连接一个nil值,会报错 attempt to concatenate a nil value
print(type .. " function") -- 不能连接一个函数,会报错 attempt to concatenate global 'type' (a function value)
print("15" + 20) -- 35 , 使用+的时候,会将数字字符串转为数字进行运算
print("a" + 10) -- attempt to perform arithmetic on a string value
求长度运算符#
#
井号可以求字符串的长度,也可以求表中的元素个数。
t = {
1, 2, "a", true}
print(#t) -- 4
print(#t[3]) -- 1
print(#"chenpi") -- 6
-- 遍历表中所有元素
for i = 1, #t do
print(t[i])
end
借助井号,我们可以向数组(表如果所有键都是数字可认为是数字)尾部添加元素。
t = {
"a", "b", "c"}
t[#t + 1] = "d"
t[#t + 1] = "e"
for i = 1, #t do
print(t[i])
end
-- 输出 a b e d e
位操作运算符
print(1 << 3) -- 8 位左移
print(8 >> 2) -- 2 位右移
print(2 | 1) -- 3 位异或
print(4 & 5) -- 4 位与
print(~2) -- -3 位取反
本次分享到此结束啦~~
我是陈皮,一个在互联网 Coding 的 ITer。如果觉得文章对你有帮助,点赞、收藏、关注、评论,您的支持就是我创作最大的动力!
版权声明
本文为[陈皮的JavaLib]所创,转载请带上原文链接,感谢
https://javalib.blog.csdn.net/article/details/124286313
边栏推荐
猜你喜欢
Install redis and deploy redis high availability cluster
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
Website pressure measurement tools Apache AB, webbench, Apache jemeter
volatile的含义以及用法
Jour (9) de ramassage de MATLAB
Interview question 17.10 Main elements
撿起MATLAB的第(9)天
Read the meaning of serial port and various level signals
Nanny Anaconda installation tutorial
Day (7) of picking up matlab
随机推荐
[key points of final review of modern electronic assembly]
How to upgrade openstack across versions
C, calculation method and source program of bell number
基于GPU实例的Nanopore数据预处理
Intersection, union and difference sets of spark operators
The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?
Construction of esp32 compilation environment
PS为图片添加纹理
Ice -- source code analysis
R语言中实现作图对象排列的函数总结
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
Function summary of drawing object arrangement in R language
JIRA screenshot
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
Day 10 abnormal mechanism
js正则判断域名或者IP的端口路径是否正确
最详细的背包问题!!!
Method 2 of drawing ROC curve in R language: proc package
Sortby use of spark operator