while 循环
在月之脚本中的 while 循环支持几种不同的写法:
i = 10while i > 0 print i i -= 1
while running == true do my_function!i = 10while i > 0 print i i -= 1
while running == true do my_function!i = 10until i == 0 print i i -= 1
until running == false do my_function!i = 10until i == 0 print i i -= 1until running == false do my_function! 像 for 循环的语法一样,while 循环也可以作为一个表达式使用。while / until 循环表达式支持 break 返回多个值。
value, doubled = while true n = get_next! break n, n * 2 if n > 10value, doubled = while true n = get_next! break n, n * 2 if n > 10为了使函数返回 while 循环的累积列表值,必须明确使用返回语句返回 while 循环表达式。
repeat 循环
Section titled “repeat 循环”repeat 循环是从 Lua 语言中搬过来的相似语法:
i = 10repeat print i i -= 1until i == 0i = 10repeat print i i -= 1until i == 0 repeat 循环表达式同样支持 break 返回多个值:
i = 1value, scaled = repeat break i, i * 100 if i > 3 i += 1until falsei = 1value, scaled = repeat break i, i * 100 if i > 3 i += 1until false