-2

I have a loop in lua and when a certain thing happens I want it to start the loop over. However when I do return it just ends the loop.

wrong = false
while true do
    if wrong then
      return
    end

    print 'Not wrong'
end
  • 3
    Are you looking for a "continue" keyword? If so, you may want to read this: http://stackoverflow.com/questions/3524970/why-does-lua-have-no-continue-statement – Alex Riley Dec 03 '16 at 23:01
  • 1
    `return` is doing exactly what it's supposed to do. In most languages `return` terminates the current function call, not just the current loop. I suggest you rewrite the question to describe what you want to do, rather than what you incorrectly think `return` should do. – Keith Thompson Dec 03 '16 at 23:25

2 Answers2

2

First of all return does not terminate your loop, it terminates the entire function / script. Be aware of that! To terminate a loop use break. You are looking for an equivalent to "continue" in other languages.

There is no such thing in Lua. You could fiddle something together using goto statements which are available in newer Lua versions but in general you could simply rewrite your code:

while true do
  if not wrong then
    print("not wrong")
  end
end
Piglet
  • 27,501
  • 3
  • 20
  • 43
0

Return terminates a function and returns a value. I believe you want to break out of a loop

warspyking
  • 3,045
  • 4
  • 20
  • 37