i = 1
while i <= 5 do
i = i + 1
print(i)
end
--[[ Repeat its body until the condition is true. It will always run at least
once. ]]
i = 5
repeat
i = i - 1
print(i)
until i == 1
-- There are two different versions of for loops.
-- for var = start, limit, step do
for i = 1, 11, 2 do
x = x * i
print(x)
end
-- table for loop
a = { x = 400, y = 300, [20] = "foo" }
b = { 20, 30, 40 }
for key, value in pairs(a) do
print(key, value)
end
--[[ ipairs is like pairs except it only loops over items with indices (starting
at 1) and stops when it encounters a nil value. ]]
for index, value in ipairs(b) do
print(index, value)
end
-- You can terminate from a loop with break, but there is no continue keyword.
while true do
if condition then
x = x ^ 2
else
break
end
end