亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

Lua goto 語句

Lua 循環(huán)

Lua 語言中的 goto 語句允許將控制流程無條件地轉(zhuǎn)到被標(biāo)記的語句處。

語法

語法格式如下所示:

goto Label

Label 的格式為:

:: Label ::

以下示例在判斷語句中使用 goto:

示例 1

local a = 1
::label:: print("--- goto label ---")

a = a+1
if a < 3 then
    goto label   -- a 小于 3 的時(shí)候跳轉(zhuǎn)到標(biāo)簽 label
end
輸出結(jié)果為:
--- goto label ---
--- goto label ---

從輸出結(jié)果可以看出,多輸出了一次 --- goto label ---。

以下示例演示了可以在 lable 中設(shè)置多個(gè)語句:

示例 2

i = 0
::s1:: do
  print(i)
  i = i+1
end
if i>3 then
  os.exit()   -- i 大于 3 時(shí)退出
end
goto s1

輸出結(jié)果為:

0
1
2
3

有了 goto,我們可以實(shí)現(xiàn) continue 的功能:

示例 3

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

輸出結(jié)果為:

1   yes continue
i'm end
2   yes continue
i'm end
3    no continue
i'm end

Lua 循環(huán)