当前位置:网站首页>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
边栏推荐
- Summary according to classification in sail software
- Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
- G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
- Website pressure measurement tools Apache AB, webbench, Apache jemeter
- ESP32编译环境的搭建
- Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function
- 捡起MATLAB的第(5)天
- 299. Number guessing game
- [section 5 if and for]
- ESXi封装网卡驱动
猜你喜欢
What is the experience of using prophet, an open source research tool?
Day (9) of picking up matlab
matplotlib教程05---操作图像
Spark 算子之groupBy使用
糖尿病眼底病变综述概要记录
撿起MATLAB的第(9)天
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
Sortby use of spark operator
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
随机推荐
Es common query, sorting and aggregation statements
Filter usage of spark operator
[self entertainment] construction notes week 2
Matplotlib tutorial 05 --- operating images
What is the experience of using prophet, an open source research tool?
Day (10) of picking up matlab
Config learning notes component
Day (3) of picking up matlab
Findstr is not an internal or external command workaround
Using JSON server to create server requests locally
捡起MATLAB的第(9)天
Website pressure measurement tools Apache AB, webbench, Apache jemeter
What is cloud migration? The four modes of cloud migration are?
R语言中绘制ROC曲线方法二:pROC包
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
Distinct use of spark operator
Day (2) of picking up matlab
JS regular determines whether the port path of the domain name or IP is correct
Start Oracle service on Linux