Repetition with loops
Loops run a block more than once. A while loop checks its condition before every iteration:
namespace Countdown
function main() local number = 3
while number > 0 do print(number) number = number - 1 end
print("Go!")endThis program prints 3, 2, 1, and then Go!. When number > 0 becomes false, execution continues after end.
Because the condition is checked first, a while body might run zero times:
local number = 0while number > 0 do print(number) -- never runsendAs with if, a loop condition must be a Boolean.
Running the body at least once
Section titled “Running the body at least once”A repeat loop checks its condition after the body:
local number = 1
repeat print(number) number = number + 1until number > 3The body runs for 1, 2, and 3. Even when the condition is already true in principle, a repeat body always runs once before Pop checks until.
A local declared in the body remains available to the until condition, which is useful when the condition depends on work just performed:
repeat local next = calculateNext() use(next)until finished(next)That local is not available after the loop finishes.
Inclusive numeric ranges
Section titled “Inclusive numeric ranges”Use numeric for when an integer moves from one bound to another:
for index = 1, 5 do print(index)endThe range includes both bounds. An optional third expression sets the step:
for even = 2, 10, 2 do print(even)end
for countdown = 3, 1, -1 do print(countdown)endThe bounds and step are evaluated once from left to right and must have one identical fixed integer type. The loop binding is body-local and immutable. A zero step is invalid; checked progression can trap on overflow.
Generalized for value in collection iteration is not implemented in rc.3. Arrays can use a numeric range from 1 through Array.length(values); the planned generalized form waits for the nominal Iterable<T> and Iterator<T> protocols.
Leaving or skipping
Section titled “Leaving or skipping”break exits the innermost loop. continue advances it to its natural condition or range step:
for number = 1, 10 do if number == 3 then continue elseif number > 6 then break end
print(number)endBoth statements work in while, repeat, and numeric for loops. They cannot cross a nested function boundary or appear outside a loop.
