Loops
Numeric For
Section titled “Numeric For”for i: integer = 1, 10 do print(i)end
-- with stepfor i: integer = 0, 100, 5 do print(i)end
-- counting downfor i: integer = 10, 1, -1 do print(i)endFor Each
Section titled “For Each”local names: table<string> = {"Arthur", "Merlin", "Lancelot"}
for name: string in names do print(name)end
-- with indexfor i: integer, name: string in names do print(i .. ": " .. name)end
-- types are optional — inferred from the iterated valuefor name in names do print(name)endlocal hp: integer = 100
while hp > 0 do hp = hp - 10endRepeat Until
Section titled “Repeat Until”Runs at least once, checks the condition at the end:
local tries: integer = 0
fn getInput() -> string? tries = tries + 1 if tries < 3 then return nil end return "ready"end
local input: string? = nil
repeat input = getInput()until input != nil
println(input ?? "") -- readyprintln(tries) -- 3, the body always ran at least onceBreak and Continue
Section titled “Break and Continue”for i: integer = 1, 10 do if i == 5 then continue end if i == 8 then break end print(i) -- prints 1, 2, 3, 4, 6, 7end