Any replace for "continue" statement in Lua loops?

Hi friends
if I’m not wrong LUA doesn’t have “continue” statement like other languages. I see some people use goto in their codes instead of “continue”, using goto in languages like C is not popular and lot of programmers avoid “goto”. is it the same for LUA too? defold document mentions to keep our code cross platform it’s better to stick to lua 5.1 ( goto added to lua since 5.2 ) do you have any suggestion or replace in cases we use continue in other languages?

I think Defold uses Lua 5.1 on platforms that don’t support LuaJIT, so no goto. I just wrap the thing that would go after the goto into an if. It adds an extra indentation level, but it’s still ok. Also generally whenever you feel like using continues, it means your code became too complex to use a plain if, so you should break it down in functions anyway.

4 Likes

Yeap, unfortunately Lua doesn’t have it. But you can try this workaround:

for i = 1, 5 do repeat
  -- do your code
  if i == 3 then
    do break end -- simulate continue
  end
  -- do your code
  print(i)
until true end

-- Output:
-- 1
-- 2
-- 4
-- 5
3 Likes