当前位置:网站首页>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
边栏推荐
- Day (5) of picking up matlab
- Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
- Master vscode remote GDB debugging
- Day (9) of picking up matlab
- Simple usage of dlopen / dlsym / dlclose
- 通过Feign在服务之间传递header请求头信息
- Meaning and usage of volatile
- Day (7) of picking up matlab
- Change the icon size of PLSQL toolbar
- VIM uses vundle to install the code completion plug-in (youcompleteme)
猜你喜欢

You need to know about cloud disaster recovery

Cloud migration practice in the financial industry Ping An financial cloud integrates hypermotion cloud migration solution to provide migration services for customers in the financial industry

Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago

Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions

Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification

Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function

各大框架都在使用的Unsafe类,到底有多神奇?

Gartner 发布新兴技术研究:深入洞悉元宇宙

Install redis and deploy redis high availability cluster

C语言自编字符串处理函数——字符串分割、字符串填充等
随机推荐
Six scenarios of cloud migration
Unity shader learning
Intersection, union and difference sets of spark operators
Implement default page
Using JSON server to create server requests locally
451. 根据字符出现频率排序
MySQL的btree索引和hash索引区别
linux上啟動oracle服務
Sail soft calls the method of dynamic parameter transfer and sets parameters in the title
ES常用查询、排序、聚合语句
Ice -- source code analysis
RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page
5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
OAK-D树莓派点云项目【附详细代码】
js正則判斷域名或者IP的端口路徑是否正確
How important is the operation and maintenance process? I heard it can save 2 million a year?
Day (10) of picking up matlab
基于GPU实例的Nanopore数据预处理
通过Feign在服务之间传递header请求头信息
The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?