English Is there goto statement?

11 replies
Goto Page
To the start Previous 1 Next To the start
23.02.20 02:42:51 pm
Up
The kennyS
User
Offline Off
Is there goto statement in lua, If there is then how to use it and what it is for? Thanks!
23.02.20 02:51:15 pm
Up
TrialAndError
User
Offline Off
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 (11).

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

Code:
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
23.02.20 03:15:35 pm
Up
DC
Admin
Offline Off
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
23.02.20 10:01:04 pm
Up
The kennyS
User
Offline Off
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?
23.02.20 10:37:47 pm
Up
TrialAndError
User
Offline Off
@user The kennyS: If you read 1 more sentence after what you just quoted, you'll get your answer.
24.02.20 12:13:21 am
Up
DC
Admin
Offline Off
@user The kennyS: Depends on what you want to do. Show the script you need goto for and we can show you how to do it without goto.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
24.02.20 08:28:04 am
Up
ohaz
User
Offline Off
user The kennyS 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
https://ohaz.engineer - Software Engineering
24.02.20 03:09:40 pm
Up
The kennyS
User
Offline Off
@user DC: Okay, here it is.

Edited user TrialAndError's sample.
Code:
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?
24.02.20 04:09:23 pm
Up
Gaios
Security Supporter
Offline Off
@user The kennyS: 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.
24.02.20 07:38:26 pm
Up
DC
Admin
Offline Off
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 kennyS: 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.

Code:
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
24.02.20 08:15:45 pm
Up
The kennyS
User
Offline Off
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.
24.02.20 08:55:14 pm
Up
DC
Admin
Offline Off
It's not easily possible and I don't see a good reason to put my time into this topic.
See thread cs2d Update LUA
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
To the start Previous 1 Next To the start