For and While Loops
Lua supports multiple looping structures: while
, repeat-until
, and for
.
While Loop:
i = 1
while i <= 5 do
print(i)
i = i + 1
end
Repeat-Until Loop:
This loop runs at least once and continues until the condition becomes true.
i = 1
repeat
print(i)
i = i + 1
until i > 5
Numeric For Loop:
for i = 1, 5 do
print(i)
end
Generic For Loop:
Used for iterating over tables:
fruits = {"apple", "banana", "cherry"}
for index, value in ipairs(fruits) do
print(index, value)
end