lua 语句学习
就如同C里的if else,while,do,repeat。就看lua里怎么用:
1、首先看if else
t = {1,2,3}
local i = 1
if t[i] and t[i] % 2 == 0 then
print("even")
else
print("odd")
end
lua木有C里的&&,而是and来表示;if 之后跟表达式,之后要更个then 最后语句结束都要写end 表示这个chunk结束了。
2、while
while t[i] do
print(t[i])
i = i + 1
end
3、do end
do
print(t[i])
i = i + 1
end
4、repeat until
local i = 1
repeat
print(t[i])
i = i + 1
until t[i] == nil
5、for的使用方法
for i = 1, #t do
print(t[i])
end
当然for循环里还能够直接调用lua API
for k,v in pairs(t) do
print(k , v)
end
6、break使用方法
for k,v in pairs(t) do
print(k , v)
if k == 2 then
break
end
end
课后题解答:
1、lua为毛支持elseif这样的写法
比方能够这么写:
for k,v in pairs(t) do
if k == 2 then
print('111')
elseif k == 1 then
print(k , v)
end
end
这样整个if ...end是一个chunk;假设if else分开写,就得这样:
t = {1,2,3}
for k,v in pairs(t) do
if k == 2 then
print('111')
else if k == 1 then
print(k , v)
end
end
end
多出了一个end。
由于lua里没有switch,假设选择分支比較多写那么多end非常丑陋,so就能够elseif一起用啦。
2、写无条件运行代码,假设c里我喜欢用do{}while(false);lua就能够有非常多写法,来个最easy想到的:
local i = 0
repeat
i = i + 1
print(i)
if i > 10 then
break
end
until false
3、第三个问题,我已经用第二个解答回答了,在lua里是要用到repeat until来做的。
4、改动恶心的goto语句,的确看着非常不爽。改了就清爽多了:
function room1()
repeat
local move = io.read()
if move == "south" then
return room3()
elseif move == "east" then
return room2()
else
print("invalid move")
end
until false
end
function room2()
repeat
local move = io.read()
if move == "south" then
return room4()
elseif move == "west" then
return room1()
else
print("invalid move")
end
until false
end
function room3()
repeat
local move = io.read()
if move == "north" then
return room1()
elseif move == "east" then
return room4()
else
print("invalid move")
end
until false
end
function room4()
print("Congratulations, u win!")
end
room1()
5、goto的限制
goto不能调到某个语句块内。由于内部是不能为外部所知道的。为毛不能跳出函数块,书里的解释是:
stackflow的答案是:
Your guesses are hinting at the answer. The reason is because the goto statement
and its destination must reside in the same stack frame. The program context before and after the goto need
to be the same otherwise the code being jumped to won't be running in its correct stack frame and its behavior will be undefined. goto in
C has the same restrictions for the same reasons.
6、这道题看到goto就好凌乱,我压根就打算用goto。也驾驭不了介个,so不分析了。
lua 语句学习的更多相关文章
- lua本学习笔记功能
Lua本学习笔记功能 1. 函数返回 指定任务的主要功能是完成,在这种情况下,函数被用作调用语句.函数可以计算并返回值,在这种情况下,作为分配值表达式语句使用. 语法: funcationfunc_ ...
- 【java开发】分支语句、循环语句学习
一.Java分支语句类型 if-else 语句 switch 关于if-esle语句可以拆分为三种 if语句 if(条件){语句块;} if-else语句if(条件语句){语句块;} if-else ...
- SQL语句学习手册实例版
SQL语句学习手册实例版 表操作 例1 对于表的教学管理数据库中的表 STUDENTS ,可以定义如下: CREATE TABLE STUDENTS (SNO NUMERIC (6, ...
- SQL 语句学习
Sql语句学习 一. select playerId, count(playerId) as num from OperateLog_$i where playerId > 0 and roo ...
- Python第一周基本语句学习整理
目录 Python第一周基本语句学习整理 一.字符串切割 二.体重单位转换 三.计算器的实现 四.猜年龄游戏 Python第一周基本语句学习整理 一.字符串切割 代码实现: s = 'luowenxi ...
- 1903021121—刘明伟—Java第四周作业—java分支语句学习
项目 内容 课程班级博客链接 19信计班(本) 作业要求链接 第四周作业 要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果). 扩展阅 ...
- 《Lua程序设计》第4章 语句 学习笔记
Lua中的常规语句包括:赋值.控制结构和过程调用.Lua还支持一些不太常见的语句,如:多重赋值(multiple assignment) 和 局部变量声明.4.1 赋值Lua允许“多重赋值”,也就是一 ...
- GJM : Lua 语言学习笔记
Lua笔记 容易与C/C++整合 Lua所提供的机制是C所不善于的:高级语言,动态结构,简洁,易于测试和调试. Lua特有的特征: `1:可扩展性.卓越的扩展性导致了很多人将Lua用作搭建领域语言的工 ...
- [2017.02.07] Lua入门学习记录
#!/home/auss/Projects/Qt/annotated/lua -- 这是第一次系统学习Lua语言 --[[ 参考资料: 1. [Lua简明教程](http://coolshell.cn ...
随机推荐
- js数组方法详解
Array对象的方法-25个 /*js数组方法详解 */ /* * 1 concat() 用于连接多个数组或者值-------------- * 2 copyWithin() 方法用于从数组的指定位置 ...
- 【3dsMax安装失败,如何卸载、安装3dMax 2012?】
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- VUE中toast的使用与开发
在这篇文章中介绍了toast是什么,这篇文章主要介绍toast的开发与使用. 开发 Vuejs很流行,并且官方也给出了路由插件vue-router.数据管理插件vuex,但是我们仅仅停留在了使用的阶段 ...
- mac下安装win7虚拟机和导入linux虚拟机
- FZU 1884——排火车——————【栈的模拟】
排火车 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- Cannot connect to VM com.sun.jdi.connect.TransportTimeoutException
Cannot connect to VM com.sun.jdi.connect.TransportTimeoutException 描述 在用eclipse进行debug的时候弹出了上面的错误,在s ...
- 微信小程序支付c#后台实现
今天为大家带来比较简单的支付后台处理 首先下载官方的c#模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在WxPayAPI项目目录中添加两个“一般处理程序” (改名为GetOp ...
- 0、安装Ionic2
1.安装ionic2 $ npm install -g ionic@beta 2.创建项目 $ ionic start 项目名称 --v2 // 默认tabs的模板 $ ionic start 项目 ...
- OpenLayers3 实现自定义放大缩小滑块,自定义方向按钮
由于项目需要,需要自定义滑块.为此记录如下: <div id="map" class = "map"> <div id = "zoo ...
- 4.kafka的安装部署
为了安装过程对一些参数的理解,我先在这里提一下kafka一些重点概念,topic,broker,producer,consumer,message,partition,依赖于zookeeper, ka ...