Lua脚本语言基本语法快速入门教程(lua脚本代码大全)原创

随心笔谈3年前发布 admin
215 0 0

文章摘要

这篇文章主要介绍了lua编程语言的基础知识和语法结构。通过一系列代码示例,文章演示了以下内容: 1. **变量赋值与条件判断**:展示了变量的使用、条件语句(if-else)的结构,以及变量值的比较。 2. **循环语句**:使用`do`语句实现了循环结构,并结合变量重新赋值进行了操作。 3. **函数定义与调用**:定义了函数并进行了参数传递和返回值的处理。 4. **表(Table)的操作**:演示了表的赋值、修改以及索引操作。 5. **记录(Record)的使用**:展示了记录的定义、赋值和属性的引用。 6. **函数作为变量的使用**:探讨了如何将函数存储在变量中,并对其进行操作。 文章通过具体的代码示例,帮助读者理解lua编程的基本语法和编程思路。


–begin
a=–[[explain]] “ha”;
print(a)

if a==”ha” then
print(“if test passed”)
else
print(“if used wrong”)
end

b,c=2,3
print(b,c)
b,c=c,b
if b==3 and c==2 then
print(“swap test passed”)
print(b,c)
else
print(“swap error”)
end

do
b=6
if b==6 then
print(“code block test passed”)
else
print(“code block test error”)
end
end

do
d=true;
local e=”haha”
end
if d==true and e==nil then
print(“local test passed”)
else
print(“local test error”)
end

c=2^3
if c==8 then
print(“squert test passed”)
else
print(“test error”)
end

a=”string will be “..”connected”
print(a)

x=x or a –if not x then x=v end
print(x)

print(type(asdf))

c=3-1.2;
print(c)

d=[[
怎么会
怎么会
你竟原谅了我?

]]
print(d)

function test (w)
print(“the num is “..w)
local add=w+1
return add;
end

b=test(5)
print(add,b)

t={
100,
[100]=”I’m the 100th element”,
fsy={
[‘age’]=22,
sex=”male”, –如果是字符串,可以去掉引号和括号
},–元素之间必须用,隔开
20, — 相当于t[2]=20
}

print(t[0])
print(t[1])
print(t[100])
print(t.fsy.age)
print(t[2])

g={
age=3,
add=function (s,n) s.age=s.age+n end
}

g:add(10) –相当于g.add(g,10)
print(g.age)

© 版权声明

相关文章