Add 'goto' and 'continue'

Both are very useful to have.

e.g.

for a = 1, 5 do
     if a == 2 then continue end
     print(a)
end
::done::
for a = 1, 5 do
     if a == 2 then goto done end
     print( a )
end
11 Likes

Aren’t gotos and continues not really used anymore because functions fill their purpose?
The only place I can think where one would be needed is jumping out of a nested loop, but you can still do that by wrapping the loop in a function

I personally use continue all the time in gmod lua. It’s probably just preference.

Both are very handy to have. (and continue can be done using labels iirc)

1 Like

I couldn’t resist the obligatory xkcd.

Although you did show pretty decent use cases, simple goto can easily cause pretty great havoc, mess up states in a way that you wouldn’t expect. It feels very hacky and makes the code harder to read for others.

9 Likes

While that’s a good concern, you don’t need goto to screw up code at all.

1 Like
for a = 1, 5 do
    if a == 2 then break end
    print( a)
end

Am I missing something? I can see a use for continue but you could also do

table.foreachi(tab, function (a)
    if a == 2 then return end
    print(a)
end)

Of course this only works when iterating tables, but you could easily create a function for this.

Those werent use cases, just examples of how they were used :stuck_out_tongue:

1 Like

Also, the ability to use goto to go backwards is something that can’t be done without rewriting your code partially.

1 Like

Alternatively, you can also do

for a = 1, 5 do
	for _ = 1, 1 do
		if a == 2 then break end
		print(a)
	end
end

It’s not very pretty but it should always work.

I believe the following is the fastest way ( speed wise ) to mimic continue in lua currently as (AFAIK) it translates into one instruction. Even so, continue would be a heck of a lot prettier and nicer to use.

for a = 1, 5 do
     repeat
          if a == 2 then do break end end
          print( a )
     until true
end
1 Like

In my opinion and a lot of other peoples opinion goto can be seen as bad practice and lead to spaghetti code.

Sure there are a few cases where it could be used effectively but I imagine most people would use it incorrectly and I would hate to read their code.

3 Likes

“Goto”

aaaaaaaaaa no

3 Likes

To be honest the main one i want is continue, however, just because you personally don’t like goto or despise it’s presence doesn’t mean others don’t find it wonderfully helpful.

goto causes readability to go out the window and makes it difficult to discern code flow. no support.

2 Likes

I want gofrom. It assumes control of a past reality event at a certain position. Great for debugging.

14 Likes

Tbh, once you’re a bit used to it, I don’t see what’s wrong with it? Badly formatted code will stay badly formatted code. Labels wouldn’t make well formatted code suddenly a mess.

2 Likes

You neither need to use them nor any code written with them, I don’t see the problem.

You don’t need to write code with comments, but not to do so is a bad time for you should you return to the project a while later. I think there are many more things you could say than defaulting to “if you don’t like it then don’t use it”.

The issue I have with this is that people will use these things in substitution of while loops, for loops, and function calls. Goto has a pretty niche use imo.

That’s the opposite of what I said.

An equal comparison would be me saying “Add comments” and you saying “I don’t like comments”.

1 Like