Continue in loops

Well, if 2LL will put stock Lua, that’s fine. But at least 2LL shouln’t encourage goto usage by not mention it in the docs nor in the samples. Keep it there in the dark corners somewhere common users won’t see it, but still let the advanturer users to find it. :wink:

Just wanted to mention this: Why I don’t want goto():
whynottousegoto

Lua has tail-call optimisation (http://en.wikipedia.org/wiki/Tail_call), which means tail calls are gotos. You can rewrite an iterative algorithm that would use break/continue as a recursive function and Lua will run it pretty much as fast as a loop (same computational complexity, any way), but the code will better express what you are calculating because the Lua interpreter takes care of how the calculation is computed.

Nat, but changing loops into recursive functions makes the code messier, longer, less natural. The main advantage of continue to me is that the code becomes shorter. Continue statements are never strictly needed, they’re just nice and useful.

Edit: break statements are also not needed, but lua has them. It is truly bizarre to me that lua doesn’t have continue.

How about a while…wend loop?

The only use of goto I can condone is for breaking out of a nested loop structure.

loop a
   loop b
     if done
        goto break_loop

break_loop:

Its cleaner than using a temporary variable and checking it in each nested loop’s invariant (especially so if you have even more nesting then 2 levels).

That said, lua uses gotos internally when the error function is called from a cfunction, jumping outside of the interpreter and printing the error. They have wrapped it in a well defined interface, with well defined conventions like always putting a return after the error function to indicate that your calling function will not continue executing, even though the return will never be called, so it works well.

It seems like the most elegant solution in that case.

When it comes to continue, you could structure your code like so:
Instead of:

loop a
  do stuff
  if skip
     do the other thing
     continue
  end
  do more stuff
end

it could become

loop a
   do stuff
   if skip
      do the other thing
   else
      do more stuff
   end
end

There is no real need for continue as far as I can see, though there are some cases (multiple exit points) where continue would probably be nicer. If that is the case though, you are probably better off using Tail Recursion like @Nat mentioned and using return instead of continue. I’ve always found recursive functions easier to reason about anyway, but its a matter of opinion.

2.5 years passed. Still no goto in Codea? WTF?!

Not needed

For you that may be.

You can tackle your situation without it believe me

If goto is used properly, it’s no different than any other command. If used improperly, it can be a disaster in a large program when changes are required. Before I retired, I was the only programmer that used goto. That was because the younger programmers learned to program with a language/compiler that didn’t allow goto’s. So they never learned the proper way to use it and kept telling me I was wrong to use it. I had no problems with it. Codea doesn’t allow goto’s, so I have no problems not using it. It’s all a matter of what you get used to.

Yeah, noobs keep saying it’s not good, when it’s actually them who are not good. Exactly what I had in mind, Dave.

Goto is fool programmers’ code particle (I’m not saying, I’m against it) :smiley:

Edit: goto can be a safe valve… Or an evil sink >:)

Did I sound a bit religious or mystical? [-O<

There’s no case for Goto being necessary, because modern languages don’t have it, and nobody notices. I’m afraid the professional concensus is that it was dangerous, so unless you think you know better than all the experts, it’s brave to say that this is stupid.

(I go back a long way with basic, so I know Goto well. But I stopped using it many years ago, because I realised it did make coding more dangerous. I never missed it after that).

Do you know what Donald Knuth thinks about “goto”? :wink:

Edit: is “goto” like a gun? 8-X

@Ignatz Actually every language has a goto. It’s just renamed as continue, break, function, if then else, etc., and it limits where you can go to, so the programmer doesn’t mess things up too bad.

Re Knuth: there’s no “goto” in TeX. 'Nuff said.

I guess all that really matters is that Codea doesn’t have goto, never will have, end of story…

@Ignatz: Well…unless TLL decides to upgrade Codea to use Lua 5.2 or greater.

You’re forgetting the biggest goto that Codea has. It’s called restart. That’s just like the Longjmp and Setjmp commands in “C”. It’s just that we can’t access Setjmp which is always set to the beginning of the code, so Longjmp (restart) always goes back to the beginning.