while / repeat...until
Lua provides two conditional loop constructs: while, which evaluates the condition first, and repeat...until, which always executes the loop body at least once before evaluating the condition. This page covers both constructs, exiting a loop with break, and the infinite loop pattern.
Syntax
-- -----------------------------------------------
-- while statement (pre-condition loop)
-- -----------------------------------------------
while condition do
-- repeats while the condition is true
-- if the condition is false from the start, the body never executes
end
-- -----------------------------------------------
-- repeat...until statement (post-condition loop)
-- -----------------------------------------------
repeat
-- always executes at least once
-- exits the loop when the until condition becomes true
until condition
-- -----------------------------------------------
-- break to exit a loop (works in both while and repeat)
-- -----------------------------------------------
while true do -- infinite loop
if exit_condition then
break -- immediately ends the loop
end
end
-- -----------------------------------------------
-- Local variables declared inside repeat are in scope for the until condition
-- -----------------------------------------------
repeat
local input = "value" -- local variable inside the repeat block
until input == "done" -- the variable declared inside repeat can be used here
Syntax Reference
| Syntax | Description |
|---|---|
while condition do ... end | Evaluates the condition first; repeats the body while it is true (pre-condition). If the condition is false from the start, the body never executes. |
repeat ... until condition | Executes the body first, then evaluates the condition (post-condition). Exits when the condition is true, so the body runs at least once. |
break | Immediately exits a while or repeat...until loop. When loops are nested, only the innermost loop is exited. |
while true do ... end | The condition is always true, creating an infinite loop. Use break or return to exit explicitly. |
local variable inside repeat | A local variable declared inside a repeat block is included in the scope of the until condition expression. This is a Lua-specific behavior not found in other block constructs. |
Sample Code
jjk_while_repeat.lua
-- jjk_while_repeat.lua — while / repeat...until sample
-- Uses Jujutsu Kaisen characters to demonstrate each loop construct
-- -----------------------------------------------
-- while statement (pre-condition): attack while cursed energy remains
-- -----------------------------------------------
print("=== while statement (pre-condition) ===")
local cursed_energy = 5 -- Gojo Satoru's remaining cursed energy
while cursed_energy > 0 do
print("Gojo Satoru activates a technique! Remaining energy: " .. cursed_energy)
cursed_energy = cursed_energy - 1 -- Lua has no -= operator; use assignment
end
print("Out of cursed energy. Techniques suspended.")
print("")
-- -----------------------------------------------
-- repeat...until statement (post-condition): training runs at least once
-- -----------------------------------------------
print("=== repeat...until statement (post-condition) ===")
local stamina = 1 -- Itadori Yuji's stamina (low from the start, but runs at least once)
repeat
print("Itadori Yuji completed one training set! Stamina: " .. stamina)
stamina = stamina + 3
until stamina >= 10 -- exit when stamina reaches 10 or more
print("Training complete! Final stamina: " .. stamina)
print("")
-- -----------------------------------------------
-- Using a local variable from repeat in the until condition
-- -----------------------------------------------
print("=== Local variable inside repeat ===")
local attempts = 0
repeat
attempts = attempts + 1
local result = attempts * 7 -- local variable inside repeat; accessible in until
print("Kugisaki Nobara attempts Straw Doll Technique (" .. attempts .. "). Result value: " .. result)
until result >= 20 -- result declared inside repeat can be used here
print("Technique stabilized. Attempts: " .. attempts)
print("")
-- -----------------------------------------------
-- Infinite loop and break: Fushiguro Megumi searches for enemies from the shadows
-- -----------------------------------------------
print("=== Infinite loop and break ===")
local enemies = { "weak curse", "weak curse", "special grade curse", "weak curse" }
local index = 1
while true do -- infinite loop
local target = enemies[index]
print("Fushiguro Megumi searching... found: " .. target)
if target == "special grade curse" then
print("Special grade confirmed! Activating Ten Shadows Technique.")
break -- exit the loop when the special grade is found
end
index = index + 1
end
print("")
-- -----------------------------------------------
-- Nested loops and break (exits only the innermost loop)
-- -----------------------------------------------
print("=== Nested loops and break ===")
local teams = { "Itadori squad", "Nanami squad" }
local members = { "Gojo Satoru", "Geto Suguru", "special grade curse" }
for _, team in ipairs(teams) do
print(team .. " roll call starting:")
local i = 1
while i <= #members do
local member = members[i]
if member == "special grade curse" then
print(" ! Intruder detected. Roll call suspended.")
break -- exits only the inner while loop; the for loop continues
end
print(" " .. member .. " -- confirmed")
i = i + 1
end
end
print("")
print("All processing complete.")
lua jjk_while_repeat.lua === while statement (pre-condition) === Gojo Satoru activates a technique! Remaining energy: 5 Gojo Satoru activates a technique! Remaining energy: 4 Gojo Satoru activates a technique! Remaining energy: 3 Gojo Satoru activates a technique! Remaining energy: 2 Gojo Satoru activates a technique! Remaining energy: 1 Out of cursed energy. Techniques suspended. === repeat...until statement (post-condition) === Itadori Yuji completed one training set! Stamina: 1 Itadori Yuji completed one training set! Stamina: 4 Itadori Yuji completed one training set! Stamina: 7 Training complete! Final stamina: 10 === Local variable inside repeat === Kugisaki Nobara attempts Straw Doll Technique (1). Result value: 7 Kugisaki Nobara attempts Straw Doll Technique (2). Result value: 14 Kugisaki Nobara attempts Straw Doll Technique (3). Result value: 21 Technique stabilized. Attempts: 3 === Infinite loop and break === Fushiguro Megumi searching... found: weak curse Fushiguro Megumi searching... found: weak curse Fushiguro Megumi searching... found: special grade curse Special grade confirmed! Activating Ten Shadows Technique. === Nested loops and break === Itadori squad roll call starting: Gojo Satoru -- confirmed Geto Suguru -- confirmed ! Intruder detected. Roll call suspended. Nanami squad roll call starting: Gojo Satoru -- confirmed Geto Suguru -- confirmed ! Intruder detected. Roll call suspended. All processing complete.
Common Mistakes
Common Mistake 1: Getting the exit condition of repeat...until backwards compared to while
while continues the loop while the condition is true, but repeat...until exits the loop when the condition becomes true. This is the opposite of C's do...while (which continues while true), so writing the wrong condition leads to unexpected behavior.
ng_example.lua
-- Writing a "continue condition" in until as if it were a while loop
local stamina = 1
repeat
print("Gojo Satoru training... Stamina: " .. stamina)
stamina = stamina + 1
until stamina <= 5 -- wrong: exits immediately because stamina <= 5 is true from the start
Gojo Satoru training... Stamina: 1
The until condition should be an "exit condition" — one that becomes true when you want the loop to stop.
ok_example.lua
local stamina = 1
repeat
print("Gojo Satoru training... Stamina: " .. stamina)
stamina = stamina + 1
until stamina > 5 -- correct: exits when stamina exceeds 5
Gojo Satoru training... Stamina: 1 Gojo Satoru training... Stamina: 2 Gojo Satoru training... Stamina: 3 Gojo Satoru training... Stamina: 4 Gojo Satoru training... Stamina: 5
Common Mistake 2: Forgetting break in a while infinite loop and causing it to run forever
while true do requires an explicit exit. Forgetting to write break, or writing logic where the exit condition never becomes true, results in an infinite loop.
ng_example2.lua
local enemies = { "weak", "weak", "special grade" }
local index = 1
while true do
local target = enemies[index]
print("Itadori Yuji searching... " .. target)
if target == "special grade" then
print("Special grade found!")
-- break is missing, so the loop continues indefinitely
end
index = index + 1
if index > #enemies then
index = 1 -- wraps back to the start, making it truly infinite
end
end
Itadori Yuji searching... weak Itadori Yuji searching... weak Itadori Yuji searching... special grade Special grade found! Itadori Yuji searching... weak Itadori Yuji searching... weak Itadori Yuji searching... special grade Special grade found! (repeats infinitely)
Place break at a position where the exit condition is guaranteed to become true.
ok_example2.lua
local enemies = { "weak", "weak", "special grade" }
local index = 1
while true do
local target = enemies[index]
print("Itadori Yuji searching... " .. target)
if target == "special grade" then
print("Special grade found! Activating Black Flash.")
break -- exit the loop when the special grade is found
end
index = index + 1
end
Itadori Yuji searching... weak Itadori Yuji searching... weak Itadori Yuji searching... special grade Special grade found! Activating Black Flash.
Overview
Lua's conditional loops come in two forms: while (pre-condition) and repeat...until (post-condition). while never executes the body if the condition is false from the start, whereas repeat...until always runs the body at least once. Note that the until condition exits the loop when it is true — the opposite of C's do...while, which continues while the condition is true. break works in both while and repeat, but in nested loops it exits only the innermost loop. A Lua-specific feature is that local variables declared inside a repeat block are in scope for the until condition expression, which is useful for input-validation loops. For iteration with a numeric counter, the numeric for loop can also be referenced.
If you find any errors or copyright issues, please contact us.