Lua While and Repeat
Loops run a block of code several times. Lua has four loop forms: while, repeat … until, the numeric for and the generic for. This lesson covers the two condition-controlled loops; the next one covers for.
while
while cond do … end checks the condition before each pass. If it is false at the start, the body never runs:
local countdown = 3
while countdown > 0 do
print(countdown .. "...")
countdown = countdown - 1
end
print("Lift-off!")3...
2...
1...
Lift-off!
A typical use is looping until something is reached, when you don't know the number of steps in advance:
-- How many years until savings double at 7% interest?
local balance, years = 1000, 0
while balance < 2000 do
balance = balance * 1.07
years = years + 1
end
print(years .. " years, balance " .. string.format("%.2f", balance))11 years, balance 2104.85
repeat … until
repeat … until cond checks the condition after each pass, so the body always runs at least once. The loop stops when the condition becomes true (the opposite of while):
local attempts = 0
local value
repeat
attempts = attempts + 1
value = attempts * attempts
until value > 20
print("stopped at", attempts, value)stopped at 5 25
A special feature: locals declared inside the repeat body are still visible in the until condition:
local n = 27
local steps = 0
repeat
local isEven = n % 2 == 0
n = isEven and n // 2 or 3 * n + 1
steps = steps + 1
until n == 1 or steps >= 200
print("Collatz steps for 27:", steps)Collatz steps for 27: 111
break
break leaves the innermost loop immediately:
local readings = { 12, 15, 19, -1, 22, 30 }
local i = 1
while true do
local r = readings[i]
if r == nil or r < 0 then
print("stop at index " .. i)
break
end
print("reading", r)
i = i + 1
endreading 12
reading 15
reading 19
stop at index 4
while true do … end with a break inside is a common pattern for "loop until something happens in the middle".
No continue
Lua has no continue statement. Either wrap the rest of the body in an if, or use goto continue with a label at the end of the body (see the goto lesson):
local i = 0
while i < 6 do
i = i + 1
if i % 2 == 0 then goto continue end
print("odd:", i)
::continue::
endodd: 1
odd: 3
odd: 5
Infinite loops
If the condition never becomes false, the loop runs forever. In the playground the run is stopped after a time limit; on your machine press Ctrl+C. The usual cause is forgetting to update the loop variable:
local i = 1
while i <= 10 do
print(i)
-- forgot: i = i + 1
endwhile vs repeat
while |
repeat … until |
|
|---|---|---|
| Condition checked | before each pass | after each pass |
| Minimum passes | 0 | 1 |
| Loop continues while condition is | true | false |
| Closing keyword | end |
until cond |
Test yourself
How many times does the body of repeat ... until true run?
- 0
- 1
- Forever
- It is a syntax error
When does a repeat ... until cond loop stop?
- When
condis false - When
condis true - After 10 passes
- Never
How do you skip to the next iteration in Lua?
continuenextgoto continuewith a::continue::labelskip