Lua DISCUSSION

Lua has no continue statement: what is the cleanest way to skip to the next loop iteration?

Started by ahmedalhazemi Lua continuegoto statementloop controlrepeat until scopeLua 5.1 compatibility
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Lua has no continue statement: what is the cleanest way to skip to the next loop iteration?

ahmedalhazemi Lua Forum
#1

I am porting a C parser loop to Lua that skips blank lines and comment lines with continue. Lua has break but no continue, and wrapping the rest of the loop body in nested if blocks is getting hard to read.

What do people normally use instead, does it work on every Lua version, and is there a technical reason the language leaves continue out?

Community replies 4

Re: Lua has no continue statement: what is the cleanest way to skip to the next loop iteration?

#2

From Lua 5.2 on, and in LuaJIT, the usual answer is goto with a label at the end of the loop body: for line in io.lines(path) do if line == "" then goto continue end ... ::continue:: end. The label name is only a convention. A label at the very end of a block is treated as being outside the scope of the block's locals, so jumping to it past local declarations is allowed.

Lua 5.1 has no goto, so this does not compile there.

Re: Lua has no continue statement: what is the cleanest way to skip to the next loop iteration?

#3

For code that must also run on 5.1, the portable forms are: invert the test and wrap the rest (if line ~= "" and not line:match("^%s*#") then ... end), or move the body into a local function and use return as the skip. Combining the skip conditions into a single guard usually removes the nesting that made it ugly.

There is also the trick of an inner repeat ... until true with break acting as continue. It works, but a break meant to leave the real loop no longer does, so I would avoid it.

Re: Lua has no continue statement: what is the cleanest way to skip to the next loop iteration?

#4

The reason usually given is the repeat ... until loop. In Lua the until condition can see local variables declared inside the loop body. A continue that jumped to the condition could skip the declaration of a local that the condition then reads.

The same rule shows up with goto: in a repeat loop, a jump to a label just before until that passes over a local declaration is rejected by the compiler with a "jumps into the scope of local" error. In for and while loops there is no such problem.

Re: Lua has no continue statement: what is the cleanest way to skip to the next loop iteration?

#5

A small pattern note for the parser itself: the blank and comment tests are clearer with anchored patterns than with string.sub comparisons. line:match("^%s*$") is true for empty or whitespace-only lines and line:match("^%s*#") for comment lines, since %s is the whitespace class and ^ anchors at the start.

With those two checks as a single guard at the top, if blank or comment then goto continue end reads almost exactly like the C original. Test it with a file containing an empty line, a line of spaces and an indented comment.

TEP COMMUNITY