lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I think that `continue` is a well established flow control keyword in programming by now. It's a little bizarre having to show its worth by different examples like it's 1989.

In my opinion, Lua would only benefit from it.

--

with reagrds, Alexander Ch
On 18.03.2023 10:26, GoldenKnightFly wrote:
`continue` is useful if you're working with input checking. For example you're checking user input through stdin, you want to check if it's valid if not do the input again.

    while true do
      io.write "Please input an ascii code: "
      local input = io.read()

      if not input:match "^%d+$" then
        print "Not a number"
        continue
      end
      
      local ok,ch = pcall(string.char,tonumber(input))
      if not ok and ch then
        print("Failed to parse code:",ch)
        continue
      end

      print("Char: "..ch)
      break
    end
    end

On Fri, Mar 17, 2023, 22:48 Steven Hall <shallnot@live.ca> wrote:
I’ve found that if-statement nesting can be easily removed by re-evaluating (refactoring if you want a better buzz-word) your logic.

You might want to come up with a more convoluted example as one can eliminate the “continue” easily here and many other such trivial examples.

for i = 1, 10 do
  if i % 2 ~= 0 then print (i) end
end

> for i = 1, 10 do
>   if i % 2 == 0 then continue end
>   print( i )
> end
>
> This should really help to reduce unnecessary if-statement nesting inside loops.