Continue Proposal

lua-users home
wiki

Add a continue statement that is used to terminate current iteration of a while, repeat, or for loop, skipping to the evaluation of the condition for the next loop iteration. Just like return and break statements, continue can only be written as the last statement of a block.

Motivation

For the official motivation for not including continue in Lua, see [1]

Pros:

Cons:

Discussion

Nearly every other language on the planet has this equivalent. Sure, you can restructure your code to get around this limitation, but it's not nice. Arguments against "continue" within loops can be equally applied to "return". Sure, with such a restriction you can restructure your code into towers of if/then/else, but it's very annoying. Being able to "return" from the middle of a code block is exactly like being able to "continue" in a loop -- it's a convenience and it doesn't hurt readability. So this is my plea for "continue". If it's simple to implement, why not? --DanHollis?

return is not quite like continue because it serves at least two additional purposes besides terminating a function: (a) identify the values returned by a function; and (b) make it easier for the compiler to identify tail calls. --RenatoMaia?

I do not understand the lack of "continue" in Lua. This is used as often as "break" in C (and other languages). Its use has benefits: it does not oblige us to find tricks to simulate it when needed (in our algorithms). I vote for it in a next version of Lua. -- JulioFernandez

I doubt that "continue" is really used as much as "break". K&R actually says it's used less often. --JohnCowan?

As C coder I'm very used to "continue" - as it often keeps loops clean and safes another unnecessary intention level. Additionally it is easy to read, understand and thus maintainable on the longrun, while the workarounds presented here are IMHO not. BTW. the linux kernel uses 106207 times the word break and 14393 times continue. So its 10% (but including all switch statements). But why completly alienate something that has 10% usage? after all. Comining as newbe to LUA this and the missing binary operators are the hugh breakdowns (and I wonder why global variables are default instead otherway around local and a keyword for global usage. But I suppose that on the other hand is far too late to fix.) --~~~~

I have just revised the patch (in LuaPowerPatches) to Lua 5.1.3, along with a small test suite to prove that it works. -- WolfgangOertl

I support this proposal. The main advantage for me is "continue" clearly and correctly expresses the sense of the algorithm, the intent of the programmer. Less complicated or shorter code is just a nice side-effect. An issue is both "continue" and "break" are ambiguous (quit current iteration or overall repetition?). For this reason, I find "next" much better for the proposed semantics. "next" is more accurate by itself and "next"+"break" resolves "break"'s ambiguity. ("next"+"exit" may be even better but it could conflict with the sense of "os.exit".) -- Denis

Emulating continue in standard Lua

Using if

Usually, you can emulate continue by changing the following code pattern

while invariant do
  <START BLOCK>
  if condition then
    <MIDDLE BLOCK>
    continue
    -- nothing is allowed here and there is
    -- no sense in adding an 'else' here
  end
  <FINISH BLOCK>
end

into

while invariant do
  <START BLOCK>
  if condition then
    <MIDDLE BLOCK>
  else
    <FINISH BLOCK>
  end
  -- here you have the chance to add a
  -- finalization code that is always executed
end

Using errors

You can do this using lua errors :) the code may looks like:

while cond do
  local b, err = pcall(function()
    ...some code...
    error("continue") -- the 'continue' equivalent :
    ...some code...
  end)
  -- if there is another error :
  if not b and err ~= "continue" then error(err) end
end

Unfortunately, you loose the stack traceback in case of errors ... you can use xpcall and debug.traceback() to keep it :) I would also prefer to have a continue statement :)

--Mildred

Using break

It's easy (though somewhat verbose) to simulate "continue" in pure Lua. --GregLudwig?

Here's a simple idiom for simulating a continue statement in Lua. The basic idea is to use a local continue variable and the break statement inside a repeat-once block.

We want implement continue as in the following example:

for line in io.lines() do

   -- start BODY
   if string.find(line,"c") then continue end

   if string.find(line,"b") then break end

   print("-->",line)
   -- end BODY

end

To accomplish this, replace BODY with:

local continue
repeat
   -- start BODY
   'BODY' replacing 'continue' with 'continue=true; break'
   -- end BODY
   continue=true
until 1
if not continue then break end

Thus the example becomes:

for line in io.lines() do

   local continue
   repeat
      -- start BODY
      if string.find(line,"c") then continue=true; break end

      if string.find(line,"b") then break end

      print("-->",line)
      -- end BODY

      continue=true
   until 1
   if not continue then break end

end

Reduced indentation at the expense of the continue functionality (i.e. always continue) can be written like:

for line in io.lines() do repeat
    if string.find(line,"c") then break end
    print("-->",line)
until 1 end

Lua 5.2.0beta-rc1

In Lua 5.2.0beta-rc1, you can write the above example using a GotoStatement like this:

for line in io.lines() do
   if string.find(line,"c") then goto continue end
   if string.find(line,"b") then break end
   print("-->",line)
   @continue:
end

Patches

See

See Also


RecentChanges · preferences
edit · history
Last edited March 27, 2012 2:32 pm GMT (diff)