石器时代LA官方

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 6383|回复: 0

[LUA教学] lua语法小结

[复制链接]

1万

主题

491

回帖

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
35504
石币
24669
发表于 2013-9-27 15:29:32 | 显示全部楼层 |阅读模式

学习lua编程的必备资料是<<programming in lua>>,如果是学习语言本身,那么可以看<<lua 5.0 reference manual>>



1.注释
lua用两个减号代码注释的开始,如:
--这个句子被注释
还有另一种方法来注释多行:
--[[
第一行被注释
第二行被注释
....
--]]

2.类型
lua是弱类型的.lua里有一种类型是nil,它代表什么都没有.未被赋值的变量都等于nil.

3.运算符
lua的运算符和C语言是差不多的.以下是常见不同之处:
1)不等:C语言是用"!="来表示,而lua是用"~="来表示.
2)逻辑运算符C语言的 "&" "|" "!" 分别对应lua的关键字 and or not
3)字符串连接符 ".."如果 str1 = "hello" .. " world."

4.函数和控制语句
lua的代码块并不是由大括号包着,例如定义函数如下:
myfunction()
--todo
end
可以看得出,关键字end代码函数结构

函数可以返回多个值.例如
myfunction()
return 1, 2;
end
local a, b = myfunction();

像if while for等语句也是通过end来表示block结束:

if condition then
--todo
end

if condition then
--todo
else
--todo
end

if condition1 then
--todo
elseif condition2 then
--todo
else
--todo
end

while condition do
--todo
end

for var = exp1, exp2, exp3 do
--todo
end

其中then和do都是关键字,不可缺少

另外,return和break是lua的关键字.跟C语言不同的是,这两个关键字只能出现在block结尾的最后一句(在end, else, until之前).如果一定要

在block中间使用这两个关键字.可以使用 do return end;或者是do break end;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|石器时代LA官方

GMT+8, 2024-4-28 08:58 , Processed in 0.091005 second(s), 21 queries .

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表