TExcelTAD
Home › Lua › Lua While and Repeat

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:

lua · try it yourself
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:

lua · try it yourself
-- 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):

lua · try it yourself
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:

lua · try it yourself
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:

lua · try it yourself
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
end
reading	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):

lua · try it yourself
local i = 0
while i < 6 do
  i = i + 1
  if i % 2 == 0 then goto continue end
  print("odd:", i)
  ::continue::
end
odd:	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:

lua
local i = 1
while i <= 10 do
  print(i)
  -- forgot: i = i + 1
end

while 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?

  1. 0
  2. 1
  3. Forever
  4. It is a syntax error

When does a repeat ... until cond loop stop?

  1. When cond is false
  2. When cond is true
  3. After 10 passes
  4. Never

How do you skip to the next iteration in Lua?

  1. continue
  2. next
  3. goto continue with a ::continue:: label
  4. skip