Loops
Contents
Lua supports repeat and while loops:
myvar = 1
while myvar <= 10 do
trace(myvar)
myvar = myvar + 1
end
The statements between do and end are executed while myvar <= 10 is true, which means that this code prints out the numbers from 1 to 10.
You can also use break to exit a loop:
myvar = 1
while true do
trace(myvar)
myvar = myvar + 1
if myvar > 10 then break end
end
This code also prints the numbers between 1 and 10.
Another way to write a loop is with the repeat statement:
myvar = 1
repeat
trace(myvar)
myvar = myvar + 1
until myvar > 10
For more detail see: control structures in the online reference manual and the Lua Tutorial wiki, or repeat and while in the Lua online programming book.
Continue: Additional Resources