r/lua • u/DrSergei • Feb 26 '26
Help Why is there NO "continue" in Lua?
I was stunlocked to find out that there is no "continue" instruction for loops in Lua. Why is that? It seems so natural to have it.
I saw some scripts where goto is used for mimicking continue statements, but It's honestly not the so;ution I would comfortably accept...
26
Upvotes
View all comments
Show parent comment
1
u/Koala_eiO Aug 11 '26 edited Aug 11 '26
Well that doesn't make it more readable. You need to negate the condition and have an extra indentation level everywhere. I'm working on something right now that could work with
for i, item in pairs(items) do____if not condition(item) then________work____endendbut would look much better with
for i, item in pairs(items) do____if condition(item) then________continue____end____workendThat's how I got here.
You see the following pattern pretty much everywhere: when you enter a function, you perform all your checks at the beginning then return prematurely or throw an error if the checks failed, and if they didn't you just move on to the real work. You don't wrap your entire work in
if there was no error in the early checks, but you could! It works work the same. It's the same argument aboutcontinue. It is to loops whatreturn(early) is to functions.