Forum

> > CS2D > Scripts > Is there goto statement?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Is there goto statement?

11 replies
To the start Previous 1 Next To the start

old Re: Is there goto statement?

TrialAndError
User Off Offline

Quote
There is a goto statement in Lua >=5.2 and in the LuaJIT version, CS2D Uses Lua 5.1, that means we cant use that so if you want to add it to your CS2D server check out file cs2d LuaJIT for Dedicated Server .

What it does is what it says, it goes (jumps) to a 'labeled' location in your code and skips over everything inbetween.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
goto gohere

local a = 3
print('This will never be printed!')

::gohere:: -- Jumps here and skips everything inbetween

-- works for both directions (up and down)

::gethere::

--This will pretty much be an infinite loop, because it runs everything inbetween here
--and then the goto statement jumps back to '::gethere::' and this part runs again

goto gethere

old Re: Is there goto statement?

DC
Admin Off Offline

Quote
Also note that goto often leads to spaghetti code. I wouldn't recommend using it at all if you're new to programming because you will be tempted to use it in places where other control structures - different kinds of loops in most cases - would make much more sense.

There are only few cases were goto is a good option (e.g. leaving nested loops). Even for these cases there are ways to do it without goto.

old Re: Is there goto statement?

The Dark Shadow
User Off Offline

Quote
user TrialAndError has written
There is a goto statement in Lua >=5.2 and in the LuaJIT version, CS2D Uses Lua 5.1, that means we cant use that

Is it possible to put this feature into CS2D?

user DC has written
Even for these cases there are ways to do it without goto.

What exactly are these ways? Could you show me an example, please?

old Re: Is there goto statement?

ohaz
User Off Offline

Quote
user The Dark Shadow has written
user TrialAndError has written
There is a goto statement in Lua >=5.2 and in the LuaJIT version, CS2D Uses Lua 5.1, that means we cant use that

Is it possible to put this feature into CS2D?
Sorry, but if you want to have gotos in your code, your code is horrible. There is no case in which you want to have goto. Write cleaner code. Forget all about goto. All of it. Don't even remember that it has existed at one point

old Re: Is there goto statement?

The Dark Shadow
User Off Offline

Quote
@user DC: Okay, here it is.

Edited user TrialAndError's sample.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
goto gohere

local a = 3
print('This will never be printed!')

function test(parameter)
   print(parameter)
end

::gohere:: -- Jumps here and skips everything inbetween

-- works for both directions (up and down)

::gethere::

--This will pretty much be an infinite loop, because it runs everything inbetween here
--and then the goto statement jumps back to '::gethere::' and this part runs again

goto gethere
@user ohaz Oh, Alright! So why did they add this useless feature?

old Re: Is there goto statement?

Gaios
Reviewer Off Offline

Quote
@user The Dark Shadow: The
goto
statement was added to enhance clarity in breaking out of nested loops, cleanly implement state machines and their close relative, interpreters.

Of course, you can't use GOTO indiscriminately. Hohndel gave a few simple rules for the correct use of GOTO:
• Only jump forward, never backward.
• Only jump to the end of a block. (It doesn't have to be the end of the function, but usually is.)
• Use good names for the label (e.g. goto error_cleanup;).
• Use it consistently.

old Re: Is there goto statement?

DC
Admin Off Offline

Quote
Actually "goto" is very old and originates from the most basic machine languages (Assembler for instance) where you did not have fancy flow control structures like if/for/while etc.
A conditional goto (if X jump to Y) is the most basic and fundamental control structure and it can be used to implement all other control structures.

People quickly noticed that goto leads to bad code in most cases and so they replaced it with better, cleaner control structures like if, else, elseif, for, while etc. These highly improve the readability of code.

I'm not sure why so many languages still allow to use goto to be honest. It's not bad in general but the risk that it is misused is very high.

@user The Dark Shadow: That sample is impracticable and makes no sense but whatever. A simple approach to simulate goto without using goto is to use a loop and a state variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- skip initial code; equal to your 'goto gohere'
local state = 2

while true
	if state == 1
		local a = 3
		print('This will never be printed!')

		function test(parameter)
			print(parameter)
		end

	-- this is your '::gohere::'
	elseif state == 2
		-- do things, continue with state 3 afterwards
		 state = 3 

	-- this is your '::gethere::'
	elseif state == 3
		-- do things. infinite loop unless state is changed inside
	end
end

In this code, setting state to 1, 2 or 3 is nearly equal to using a goto in your code. To make it entirely equal when called anywhere in the loop you also would have to use "continue" - which does not exist in Lua. Of course there are ways around that as well. e.g. put the code into methods and use return instead of continue.

The thing is: If you need to jump around in your code like crazy with goto then your code is very bad and you should restructure it.

old Re: Is there goto statement?

The Dark Shadow
User Off Offline

Quote
So I finally understood guys, It doesn't make sense to use it, At least for me. I don't see any reason to use it. Thanks guys

EDIT: One more question, Is it possible to put lua 5.3.5 into CS2D like the LuaJIT? If LuaJIT is possible so Lua 5.3.5 should be possible too? Sorry, I have no knowledge about it.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview