Replacing continue
Lesson 8 of 29 in Coddy's Lua for Luau Developers course.
Luau supports continue to skip the remaining body of the current loop. Lua 5.1 has neither continue nor goto. Wrap the work in an inclusion condition instead. Keep any operations that must run on every iteration outside that guarded section. A break still exits the nearest enclosing loop in both languages. The task uses two separate decisions: a stop marker ends processing, while nonpositive values merely skip the addition. Preserve that distinction when restructuring control flow. Numeric for bounds, while conditions and repeat-until termination already have familiar semantics; they do not need separate introductory lessons.
Luau
local total = 0
for _, n in {-2, 0, 3} do
if n <= 0 then continue end
total += n
end
print(total)Lua 5.1
local total = 0
for _, n in ipairs({-2, 0, 3}) do
if n > 0 then total = total + n end
end
print(total)Porting trap
Replacing continue with break stops at the first excluded value. It can pass a positive-only example and still lose later valid input.
Challenge
EasyInput lines are integers, with optional STOP. Sum only positive integers before the first STOP. Ignore all later lines. Empty input returns zero.
Edit app.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Try it yourself
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lua for Luau Developers
2Flow and Functions
Generalized IterationReplacing continueErasing Type SyntaxPacking Multiple ResultsPractice on your own: Online Lua compiler