Lua has three loops, and the numeric for is the one you will reach for most. It reads as from, to, by — and both ends are included.
for i = 1, 5 do io.write(" ", i) endprint()for i = 10, 1, -3 do io.write(" ", i) endprint()for i = 0, 1, 0.25 do io.write(" ", i) endprint() 1 2 3 4 5 10 7 4 1 0 0.25 0.5 0.75 1for i = 1, 5 runs five times, ending on 5. There is no i < 5 to get wrong and no i++ to forget — Lua has no increment operator at all. The third number is the step, it may be negative, and it may be a fraction.
io.write is new here. It is like print with no tabs and no line break, which is what you want when building one line out of many pieces.
Two things the loop variable will not let you do. It only exists inside the loop, so you cannot read i after the end. And assigning to it inside the loop does not change how many times the loop runs — Lua worked the count out before the first pass.
while repeats for as long as its condition holds, and checks before each pass.
local fuel = 10
while fuel > 0 do io.write(" ", fuel) fuel = fuel - 3endprint() 10 7 4 1repeat ... until is the same idea upside down. It checks after each pass, so the body always runs at least once, and the condition says when to stop rather than when to continue.
local tries = 0
repeat tries = tries + 1until tries >= 3
print("tries:", tries)tries: 3Both of those conditions are inverted relative to each other. If you ever translate a while into a repeat, the condition has to flip, and forgetting that gives you a loop that runs exactly once or never stops.
break leaves the loop immediately, in any of the three kinds.
for i = 1, 10 do if i > 4 then break end io.write(" ", i)endprint() 1 2 3 4There is no continue in Lua. This is a genuine gap, and the accepted workaround is a jump to a label at the very bottom of the loop body.
for i = 1, 6 do if i % 2 == 0 then goto continue end io.write(" ", i) ::continue::endprint() 1 3 5goto continue jumps forward to ::continue::, skipping whatever sits between. The label is not a keyword and continue is not special — you could call it ::skip:: — but everyone writes it this way, so write it this way too.
The label has to be the last thing in the body. A label with code after it means you jumped past some lines and then ran others, which is not what you meant. And you can usually avoid the whole thing by inverting the test into if i % 2 ~= 0 then — reach for the jump when the alternative is three levels of nesting.
Start a fresh main.lua. You are running a launch sequence, burning the fuel it takes, and sweeping a row of checkpoints on the way out.
for and a negative step.-- main.lua - Skynest launch sequence
for i = 5, 1, -1 do print("T-minus", i)endprint("LIFTOFF")T-minus 5T-minus 4T-minus 3T-minus 2T-minus 1LIFTOFF-- main.lua - Skynest launch sequence
for i = 5, 1, -1 do print("T-minus", i)endprint("LIFTOFF")
local fuel = 20local distance = 0
while fuel > 0 do fuel = fuel - 6 distance = distance + 10end
print("Fuel left:", fuel)print("Distance:", distance)T-minus 5T-minus 4T-minus 3T-minus 2T-minus 1LIFTOFFFuel left: -4Distance: 40The condition is checked before the pass, not during it. With 2 fuel left the loop still qualifies, then burns 6 anyway. A loop guard says whether to start a pass, never whether the pass can afford to finish.
goto continue and a label.-- main.lua - Skynest launch sequence
for i = 5, 1, -1 do print("T-minus", i)endprint("LIFTOFF")
local fuel = 20local distance = 0
while fuel > 0 do fuel = fuel - 6 distance = distance + 10end
print("Fuel left:", fuel)print("Distance:", distance)
for checkpoint = 1, 8 do if checkpoint % 2 == 0 then goto continue end print("Scanning checkpoint", checkpoint) ::continue::endgoto at all. Two ways exist: invert the condition, or change the loop's step.while into a repeat ... until that behaves the same. Remember the condition has to flip.Your finished file should produce this.
T-minus 5T-minus 4T-minus 3T-minus 2T-minus 1LIFTOFFFuel left: -4Distance: 40Scanning checkpoint 1Scanning checkpoint 3Scanning checkpoint 5Scanning checkpoint 7You now have every control structure Lua has. Everything from here is about the one data structure you have not met yet — and once you have it, loops become the thing you do to it.