Latest web development tutorials

Lua repeat…until 循環

Lua 循環 Lua循環

Lua 編程語言中repeat...until 循環語句不同於for 和while循環,for 和while循環d的條件語句在當前循環執行開始時判斷,而repeat...until 循環的條件語句在當前循環結束後判斷。

語法

Lua 編程語言中repeat...until 循環語法格式:

repeat
   statements
until( condition )

repeat...until 是條件後行,所以repeat...until 的循環體裡面至少要運行一次。

statements(循環體語句)可以是一條或多條語句, condition(條件)可以是任意表達式,在condition(條件)為true時執行循環體語句。

condition(條件)為false時會跳過當前循環並開始腳本執行緊接著的語句。

Lua repeat...until 循環流程圖如下:

實例

--[ 变量定义 --]
a = 10
--[ 执行循环 --]
repeat
   print("a的值为:", a)
   a = a + 1
until( a > 15 )

執行以上代碼,程序輸出結果為:

a的值为:	10
a的值为:	11
a的值为:	12
a的值为:	13
a的值为:	14
a的值为:	15

Lua 循環 Lua循環