LUA(初学)

条件语句if

lua 复制代码
if then
end
lua 复制代码
local a = 2
if a < 6 then
    print(a)
end
lua 复制代码
2

条件语句if else

lua 复制代码
if then
else
end
lua 复制代码
local a = 2
local b = 3
if a > 6 then
    print(a)
else
    print(b)
end
lua 复制代码
3

while循环语句

lua 复制代码
while do
end
lua 复制代码
local a = 2
while  a < 5 do
    a = a + 1
    print(a)
end
lua 复制代码
3
4
5

for循环语句

lua 复制代码
for do
end
lua 复制代码
for a = 1,10,3 do
    print(a)
end
lua 复制代码
1
4
7
10

repeat循环语句

lua 复制代码
repeat 
until
lua 复制代码
local a = 2
repeat 
   a = a + 1
   print(a)
until a > 5
lua 复制代码
3
4
5
6

文本格式化

lua 复制代码
string.format
lua 复制代码
local a = 2
local b = 3
--local c = a.."*"..b.."="..(a * b)
local c = string.format("%d*%d=%d", a, b, a*b)
print(c)
lua 复制代码
2*3=6

循环控制 break/goto

1、跳出整个循环

lua 复制代码
break--结束
lua 复制代码
for a = 1,5 do
    if a == 3 then
        break
    end
    print(a)
end
lua 复制代码
1
2

2、跳出本次循环

lua 复制代码
goto--跳转到后面代号处
lua 复制代码
for a = 1,5 do
    if a == 3 then
        goto abc
    end
    print(a)
    ::abc::
end
lua 复制代码
1
2
4
5

嵌套循环

lua 复制代码
for a = 1,9 do
    for b = 1,a do
        io.write(string.format("%d*%d=%d ", a, b, a*b))
    end
    io.write("\n")
end
lua 复制代码
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

函数定义、调用

lua 复制代码
function
end
lua 复制代码
function say()
    print("say内容1")
    print("say内容2")
end
--不会输出,需要调用(让函数执行)
say()--调用
say()
lua 复制代码
say内容1
say内容2
say内容1
say内容2

函数参数

lua 复制代码
function
end
lua 复制代码
--参数(输入):函数执行之前的已知条件,给函数传递的数据
--返回值(输出):函数执行之后带回来的结果
function say(name)--()内叫参数列表
    print("你好,"..name.."博士")
end
say("大")
lua 复制代码
你好,大博士

return函数返回值

lua 复制代码
return--中止当前函数
lua 复制代码
function say(name)
    return("你好,"..name.."博士")
    --return后不能加东西,会报错
end
local a = say("小")
print (a)
lua 复制代码
你好,小博士

函数参数、返回值

lua 复制代码
function say(a,b)--多个参数以逗号分隔
    return a*b
end
local c = say(2,3)
local d = say(10,3)
print(c,d)
lua 复制代码
6	30
--[[使用print(a, b)这样的方式输出多个参数时,参数之间会自动用制表符(tab)分隔,而不是简单的空格。
这是Lua的print函数默认行为,它会自动在每个参数之间插入一个制表符(ASCII码为9)作为分隔符]]
相关推荐
William一直在路上7 分钟前
LUA脚本语言
开发语言·lua
spencer_tseng8 分钟前
WeakAuras Lua Script ICC (BarneyICC) Simplified Chinese
lua·wow·icc
Eoneanyna26 分钟前
go与grpc
开发语言·后端·golang
啊阿狸不会拉杆40 分钟前
《算法导论》第 7 章 - 快速排序
开发语言·数据结构·c++·算法·排序算法
PyHaVolask42 分钟前
PHP进阶语法详解:命名空间、类型转换与文件操作
开发语言·php·composer
意疏2 小时前
浙江大学PTA程序设计C语言基础编程练习题6-10
c语言·开发语言
AI必将改变世界2 小时前
【软考系统架构设计师备考笔记5】 - 专业英语
java·开发语言·人工智能·笔记·系统架构·英语
listhi5202 小时前
Python实现信号小波分解与重构
开发语言·python·重构
骑驴看星星a2 小时前
层次分析法代码笔记
开发语言·笔记·python·numpy
kebeiovo4 小时前
C++实现线程池(3)缓存线程池
开发语言·c++