Control Flow Details
Break Loop
break exits the nearest loop immediately.
Break Loop
break_loop.lua
local stopAt =
local total = 0
for number = 1, 6 do
if number == stopAt then
break
end
total = total + number
end
print("stopAt=" .. stopAt)
print("total=" .. total)
local stopAt =
local total = 0
for number = 1, 6 do
if number == stopAt then
break
end
total = total + number
end
print("stopAt=" .. stopAt)
print("total=" .. total)
local stopAt =
local total = 0
for number = 1, 6 do
if number == stopAt then
break
end
total = total + number
end
print("stopAt=" .. stopAt)
print("total=" .. total)
early exit
Use `break` when the loop has found enough information and should stop.