Skip to content
Loops
step 1/5

Reading — step 1 of 5

Learn

~1 min readControl Flow

Three loop forms:

Numeric for — counted iteration:

for i = 1, 5 do
    print(i)         -- 1, 2, 3, 4, 5
end
for i = 10, 1, -1 do  -- with step
    print(i)
end

while — pre-check:

local n = 1
while n < 100 do
    n = n * 2
end

repeat ... until — post-check (loops at least once):

repeat
    line = io.read()
until line == "quit"

break exits the innermost loop. Lua does NOT have continue — use a goto or restructure with if.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Loops — Lua Fundamentals