[历史归档]本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。
发布时间:2014-08-28| 标题:Lua快速学习笔记:语句|分类:编程 / C && C++ / Lua |标签:lua·语句
Lua快速学习笔记:语句
- 备注:
- 算术操作符:
- 关系操作符:
- 逻辑操作符:
- 多重赋值语句:
- 局部变量与语句块:
- if结构:
- while语句:
- repeat..until类似于do..while:
- 数字型for语句:
- 泛型for语句:
- 关于pairs 和 ipairs:
- 测试用Return语句:
备注:
1 本笔记只记录了LUA的一小部分内容,对于LUA的描述并不全面,以后随用随增加吧。
2 本笔记参考《Lua程序设计 第二版》,截图和代码属于原作者所有。
3 作者初学LUA,经验和能力有限,笔记可能有错误,还请各位路过的大牛们给予指点。
算术操作符:
Lua没有整数和浮点数的区别,因此关于取模运算符如下图:
关系操作符:
nil只与其自身相等。
逻辑操作符:
多重赋值语句:
v1,v2=10,20;print("v1:"..v1.." v2:"..v2);-->输出v1:10 v2:20v1,v2=v2,v1;print("v1:"..v1.." v2:"..v2);-->输出v1:20 v2:10局部变量与语句块:
Local v=10;--显式界定一个语句块:dolocalv=100;print(v);-->输出100endprint(v);-->>输出nilif结构:
v1=0;ifv1<10thenprint("v1 < 10");endifv1<10thenprint("v1 < 10");elseprint("v1 >= 10");endifv1<10thenprint("v1 < 10");elseif(v1<20)thenprint("v1 < 20");elseprint("v1 >= 20");endwhile语句:
localnum=0;whilenum<4doprint(num);num=num+1;endrepeat…until类似于do…while:
localnum=5;repeatprint(num);num=num-1;untilnum==0数字型for语句:
不要在循环过程中修改控制变量的值!
泛型for语句:
这里先简单的笔记下最常用的:
tb={"A","B","C",name="Hello",[10]="D",["age"]=20};fori,vinipairs(tb)doprint(i,v);endprint("====");fori,vinpairs(tb)doprint(i,v);end输出:
1A2B3C====1A2B3C10D关于pairs 和 ipairs:
Ipairs:【 http://manual.luaer.cn/pdf-ipairs.html 】
Returns three values:an iteratorfunction,the table t,and0,so that the constructionfori,vinipairs(t)dobodyendwill iterate over thepairs(1,t[1]),(2,t[2]),···,up to the first integer key absent from the table.Pairs:【 http://manual.luaer.cn/pdf-pairs.html 】
Returns three values:the nextfunction,the table t,andnil,so that the constructionfork,vinpairs(t)dobodyendwill iterate over all key–value pairs of table t.