当前位置:网站首页>04 Lua operator
04 Lua operator
2022-04-23 16:21:00 【Javalib of tangerine peel】
List of articles
Operator
Assignment operator
Assignment operator =
One or more values can be assigned to one or more variables .
name = "chenpi"
age = 18
a, b = 10, 20
print(name) -- chenpi
print(age) -- 18
print(a) -- 10
print(b) -- 20
You can also simply interact with the values of two variables , as follows
a, b = 10, 20
print(a) -- 10
print(b) -- 20
a, b = b, a
print(a) -- 20
print(b) -- 10
Arithmetic operator
- Add :+
- Subtraction :-
- Multiplication :*
- division :/
- modulus :%
- Take the negative :-
- Exponentiation :^
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
Comparison operator
- Greater than :>
- Greater than or equal to :>=
- Less than :<
- Less than or equal to :<=
- be equal to :==
- It's not equal to :~=
print(10 > 20) -- false
print(20 >= 10) -- true
print(2 < 5) -- true
print(15 <= 2) -- false
print(15 == 12) -- false
print(12 ~= 13) -- true
Relational operator
- also :and
- perhaps :or
- Not :not
about and Operator , If the first expression is true , And the operation result of the second expression is not a Boolean value , This value will be output .
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
about or Operator , If the first expression is false , And the operation result of the second expression is not a Boolean value , This value will be output .
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 Take the , That is, the Boolean value is reversed , stay Lua in , Only false and nil representative false, Other values represent 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
Connector
Lua Use the symbol... For the connection of strings in ..
, Not like other languages +
, Plus at Lua Represents the operation .
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) -- Can't connect boolean value , Will report a mistake attempt to concatenate global 'b' (a boolean value)
print(address .. " area") -- Cannot connect to an undefined variable , Will report a mistake attempt to concatenate global 'address' (a nil value)
print(nil .. " area") -- Can't connect to one nil value , Will report a mistake attempt to concatenate a nil value
print(type .. " function") -- Cannot connect a function , Will report a mistake attempt to concatenate global 'type' (a function value)
print("15" + 20) -- 35 , Use + When , Will convert a numeric string into a number for operation
print("a" + 10) -- attempt to perform arithmetic on a string value
Find the length operator #
#
The pound sign can find the length of the string , You can also find the number of elements in the table .
t = {
1, 2, "a", true}
print(#t) -- 4
print(#t[3]) -- 1
print(#"chenpi") -- 6
-- Traverse all elements in the table
for i = 1, #t do
print(t[i])
end
With the help of well number , We can add to the array ( If all keys in the table are numbers, they can be regarded as numbers ) Add elements to the end .
t = {
"a", "b", "c"}
t[#t + 1] = "d"
t[#t + 1] = "e"
for i = 1, #t do
print(t[i])
end
-- Output a b e d e
Bitwise operators
print(1 << 3) -- 8 Bit shift left
print(8 >> 2) -- 2 Shift right
print(2 | 1) -- 3 Bit exclusive or
print(4 & 5) -- 4 Bit and
print(~2) -- -3 Bit inversion
This sharing is over ~~
I am a Dried tangerine or orange peel , One on the Internet Coding Of ITer. If you think the article will help you , give the thumbs-up 、 Collection 、 Focus on 、 Comment on , Your support is the biggest motivation for my creation !
版权声明
本文为[Javalib of tangerine peel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231613002144.html
边栏推荐
- 各大框架都在使用的Unsafe类,到底有多神奇?
- Gartner 发布新兴技术研究:深入洞悉元宇宙
- 安装Redis并部署Redis高可用集群
- Summary according to classification in sail software
- Leetcode-374 guess the size of the number
- ESP32编译环境的搭建
- Force buckle-746 Climb stairs with minimum cost
- Read the meaning of serial port and various level signals
- 捡起MATLAB的第(8)天
- js正則判斷域名或者IP的端口路徑是否正確
猜你喜欢
随机推荐
七朋元视界可信元宇宙社交体系满足多元化的消费以及社交需求
What does cloud disaster tolerance mean? What is the difference between cloud disaster tolerance and traditional disaster tolerance?
linux上啟動oracle服務
MySQL - execution process of MySQL query statement
C language self compiled string processing function - string segmentation, string filling, etc
05 Lua 控制结构
R语言中实现作图对象排列的函数总结
About JMeter startup flash back
最详细的背包问题!!!
ES常用查询、排序、聚合语句
Win11 / 10 home edition disables the edge's private browsing function
基于GPU实例的Nanopore数据预处理
Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
Day 10 abnormal mechanism
matplotlib教程05---操作图像
[section 5 if and for]
Homewbrew installation, common commands and installation path
JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory
Government cloud migration practice: Beiming digital division used hypermotion cloud migration products to implement the cloud migration project for a government unit, and completed the migration of n
捡起MATLAB的第(10)天