当前位置:网站首页>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
边栏推荐
- JSP learning 1
- Simple usage of dlopen / dlsym / dlclose
- Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
- [self entertainment] construction notes week 2
- Meaning and usage of volatile
- Findstr is not an internal or external command workaround
- C语言自编字符串处理函数——字符串分割、字符串填充等
- Nanny Anaconda installation tutorial
- Questions about disaster recovery? Click here
- 力扣-198.打家劫舍
猜你喜欢

What is cloud migration? The four modes of cloud migration are?

Sortby use of spark operator

Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant

TIA botu - basic operation

What is the experience of using prophet, an open source research tool?

一文掌握vscode远程gdb调试

Day (8) of picking up matlab

Change the icon size of PLSQL toolbar

Distinct use of spark operator

Meaning and usage of volatile
随机推荐
Construction of esp32 compilation environment
About JMeter startup flash back
PHP 零基础入门笔记(13):数组相关函数
基于GPU实例的Nanopore数据预处理
Implement default page
Day (3) of picking up matlab
The most detailed knapsack problem!!!
leetcode-374 猜数字大小
OAK-D树莓派点云项目【附详细代码】
Install redis and deploy redis high availability cluster
shell_ two
Day (2) of picking up matlab
Sortby use of spark operator
捡起MATLAB的第(5)天
Spark 算子之filter使用
Win11 / 10 home edition disables the edge's private browsing function
What is cloud migration? The four modes of cloud migration are?
Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
[key points of final review of modern electronic assembly]
Database dbvisualizer Pro reported file error, resulting in data connection failure