当前位置:网站首页>05 Lua control structure
05 Lua control structure
2022-04-23 16:21:00 【Javalib of tangerine peel】
List of articles
1 Judgment statement
The judgment block consists of if,elseif,else Keywords make up , also With end Keyword end . The result of a conditional expression can be any value .Lua Lieutenant general false and nil Be false , All other values are treated as true (0 And empty strings are also treated as true ).
-- Definition 1
if expression then
Sentence block
end
-- Definition 2
if expression then
Sentence block
else
Sentence block
end
-- Definition 3
if expression then
Sentence block
elseif expression then
Sentence block
elseif expression then
Sentence block
...
else
Sentence block
end
Definition 1 Demonstration examples are as follows :
a = 10
if a < 20 then print("ok") end
if nil then print("I am nil") end
if 0 then print("I am 0") end
function func(b)
if a == b then
print("equals")
end
end
func(10)
-- The output is as follows
ok
I am 0
equals
Definition 2 Demonstration examples are as follows :
name = "chenpi"
function func(myname)
if name == myname then
print("equals")
else
print("not equals")
end
end
func("chenpiok")
-- The output is as follows
not equals
Definition 3 Demonstration examples are as follows :
function func(age)
if age ==10 then
print("1")
elseif age == 20 then
print("2")
else
print("3")
end
end
func(20) -- 2
2 Loop statement
2.1 while…do
while The loop condition do
The loop body
end
-- Example , Output 1-5 Five numbers
local i = 1
while i <= 5 do
print(i)
i = i + 1
end
2.2 for…do
for Variable = Initial value , Termination value , step do
The loop body
end
-- Example , Output 1-5 Five numbers
for i = 1, 5, 1 do
print(i)
end
for i = 1, 5 do -- The step size can be omitted , The default is 1
print(i)
end
-- Output 5-1
for i = 5, 1, -1 do
print(i)
end
We need to pay attention to , Initial value , Termination value , Every step will be executed only once , So even if you're in the loop , It's no use changing their values .
-- The following example still only outputs 1-5 Five numbers
local x = 5
local y = 1
for i = 1, x, y do
print(i)
x = 10
y = 3
end
2.3 repeat…until
repeat
The loop body
until The loop condition
-- Example , Output 1-5 Five numbers
local i = 1
repeat
print(i)
i = i + 1
until i > 5
2.4 for…in…do
for Variable list in iterator do
The loop body
end
t = {
"a", "b", "c"}
for k,v in pairs(t) do
print(k..":"..v)
end
-- The output is as follows
1:a
2:b
3:c
We can customize an iterator .
-- Define iterator
function mypairs(t)
return myfunc, t, 0 -- Return iteration function , The set to be traversed , Control variables . The set to be traversed , The control variable will be passed into the iterative function
end
function myfunc(t, i)
i = i + 1
local v = t[i]
if v then
return i, v
end
return nil, nil
end
t = {
"a", "b", "c"}
for k,v in mypairs(t) do
print(k..":"..v)
end
-- The output is as follows
1:a
2:b
3:c
2.5 break
break Can jump out of the loop .
for i = 1, 5, 1 do
print(i)
if i == 3 then
break
end
end
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/202204231613002103.html
边栏推荐
猜你喜欢
【现代电子装联期末复习要点】
R语言中绘制ROC曲线方法二:pROC包
What is cloud migration? The four modes of cloud migration are?
下载并安装MongoDB
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
Nacos 详解,有点东西
[key points of final review of modern electronic assembly]
Intersection, union and difference sets of spark operators
撿起MATLAB的第(9)天
Sortby use of spark operator
随机推荐
ESP32_Arduino
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
OAK-D树莓派点云项目【附详细代码】
JS regular determines whether the port path of the domain name or IP is correct
Download and install mongodb
Construction of esp32 compilation environment
Sail soft calls the method of dynamic parameter transfer and sets parameters in the title
Nacos 详解,有点东西
通过Feign在服务之间传递header请求头信息
Report FCRA test question set and answers (11 wrong questions)
Passing header request header information between services through feign
The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?
如何进行应用安全测试(AST)
Implement default page
logback的配置文件加载顺序
捡起MATLAB的第(4)天
捡起MATLAB的第(5)天
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
VIM uses vundle to install the code completion plug-in (youcompleteme)
Research and Practice on business system migration of a government cloud project