当前位置:网站首页>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
边栏推荐
- 保姆级Anaconda安装教程
- About JMeter startup flash back
- JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory
- logback的配置文件加载顺序
- The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
- 捡起MATLAB的第(7)天
- 一文掌握vscode远程gdb调试
- homwbrew安装、常用命令以及安装路径
- Implement default page
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
猜你喜欢

Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons

Sortby use of spark operator

R语言中实现作图对象排列的函数总结

volatile的含义以及用法

保姆级Anaconda安装教程

RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page

面试题 17.10. 主要元素

Sail soft segmentation solution: take only one character (required field) of a string

Website pressure measurement tools Apache AB, webbench, Apache jemeter

Jour (9) de ramassage de MATLAB
随机推荐
第十天 异常机制
dlopen/dlsym/dlclose的简单用法
Compile, connect -- Notes
Day (7) of picking up matlab
TIA botu - basic operation
Unity Shader学习
Change the icon size of PLSQL toolbar
一文读懂串口及各种电平信号含义
JIRA screenshot
451. 根据字符出现频率排序
Simple usage of dlopen / dlsym / dlclose
保姆级Anaconda安装教程
力扣-746.使用最小花费爬楼梯
How to quickly batch create text documents?
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
The most detailed Backpack issues!!!
Groupby use of spark operator
Download and install mongodb
Intersection, union and difference sets of spark operators
漫画:什么是IaaS、PaaS、SaaS?