小白教程
所有教程
关于
Search
108.162.216.243
108.162.216.243
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Lua goto”的源代码
本页内容
上一节:
Lua_break_statement
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:goto 语句}}[[Category:lua loops|6]] = Lua goto 语句 = [[lua loops|Lua 循环]] Lua 语言中的 goto 语句允许将控制流程无条件地转到被标记的语句处。 === 语法 === 语法格式如下所示: <pre>goto Label</pre> Label 的格式为: <pre>:: Label ::</pre> 以下实例在判断语句中使用 goto: == 实例 1 == <sample title="" desc="" lang="lua" hererun="1"> local a = 1 ::label:: print("--- goto label ---") a = a+1 if a < 3 then goto label -- a 小于 3 的时候跳转到标签 label end </sample> 输出结果为: <pre>--- goto label --- --- goto label ---</pre> 从输出结果可以看出,多输出了一次 '''--- goto label ---'''。 以下实例演示了可以在 lable 中设置多个语句: == 实例 2 == <sample title="" desc="" lang="lua" hererun="1"> i = 0 ::s1:: do print(i) i = i+1 end if i>3 then os.exit() -- i 大于 3 时退出 end goto s1 </sample> 输出结果为: <pre>0 1 2 3</pre> 有了 goto,我们可以实现 continue 的功能: == 实例 3 == <sample title="" desc="" lang="lua" hererun="1"> for i=1, 3 do if i <= 2 then print(i, "yes continue") goto continue end print(i, " no continue") ::continue:: print([[i'm end]]) end </sample> 输出结果为: <pre>1 yes continue i'm end 2 yes continue i'm end 3 no continue i'm end</pre> [[lua loops|Lua 循环]]
返回至“
Lua goto
”。
上一节:
Lua_break_statement