AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   how to scripting goto and labels? (https://forums.alliedmods.net/showthread.php?t=153464)

schmurgel1983 03-23-2011 18:54

how to scripting goto and labels?
 
how to scripting goto and labels?

sample: dosen't work
Code:

if (blabla)
{
    if (blabla)
        goto label
    else
    {
        code
        goto label
    }
    label:
    code
    code
    code
    code
}

sample2: dosen't work
Code:

if (blabla)
{
    if (blabla)
        goto label
    else
    {
        code
        goto label
    }
    label: {
    code
    code
    code
    code
    }
}

sample3: dosen't work
Code:

if (blabla)
{
    if (blabla)
        goto label
    else
    {
        code
        goto label
    }
    label: (
    code
    code
    code
    code)
}


ehha 03-23-2011 19:18

Re: how to scripting goto and labels?
 
Sample 1 works, but not as you think.
PHP Code:

    if (bla)
        goto 
label
    
else
    {
        
code1
        
goto label
    
}
    
label:
    
code2 

So, here's the deal:
- if bla is true, we goto label and execute code 2
- if bla is false, execute code1 then goto label and execute code2

Now, if we change the code to
PHP Code:

    if (bla)
        goto 
label
    
else
    {
        
code1
    
}
    
label:
    
code2 

- if bla is true, we goto label and execute code 2
- if bla is false, execute code1 then we exit "else" block and we execute code 2
Same thing, right?

Check this to see how i've tried to use it, i've managed to perfect that plugin by not using goto, i just hope you'll see the use of it in that context.

Basically, you can use goto to jump back or forward some code, preferably many lines.

Moar stuff on goto.

Exolent[jNr] 03-23-2011 20:29

Re: how to scripting goto and labels?
 
Goto's should only be used if you have no other possible solution.
In this case, you can do it with only if() statements.

Sample 1:
PHP Code:

if (blabla)
{
    if (
blabla)
        goto 
label
    
else
    {
        
code1
        
goto label
    
}
    
label:
    
code2


Should be:
PHP Code:

if (blabla)
{
    if (!
blabla)
    {
        
code1
    
}
    
code2


Sample 2:
PHP Code:

if (blabla)
{
    if (
blabla)
        goto 
label
    
else
    {
        
code1
        
goto label
    
}
    
label: {
    
code2
    
}


Adding brackets is not the syntax for labels.

Seta00 03-23-2011 22:23

Re: how to scripting goto and labels?
 
Quote:

Originally Posted by Exolent[jNr] (Post 1438405)
Goto's should only be used if they cure cancer.

Fixed for you.

Exolent[jNr] 03-23-2011 23:03

Re: how to scripting goto and labels?
 
Quote:

Originally Posted by Seta00 (Post 1438447)
Fixed for you.

Really? Because I know that they are considered to be horrible coding, but what's the problem if there is absolutely no other solution (that of course isn't horrid)?

schmurgel1983 03-24-2011 01:36

Re: how to scripting goto and labels?
 
ah ok i understand thank you both

Seta00 03-24-2011 06:26

Re: how to scripting goto and labels?
 
Quote:

Originally Posted by Exolent[jNr] (Post 1438453)
Really? Because I know that they are considered to be horrible coding, but what's the problem if there is absolutely no other solution (that of course isn't horrid)?

There's always another solution.

rhelgeby 03-24-2011 10:48

Re: how to scripting goto and labels?
 
I can't think of any good reason to use goto. As mentioned, it's always possible with other methods. Also, goto makes it difficult to read program flow.

Maybe the compiler should mark goto as deprecated to force people to avoid (at least one) bad programming habit. :P


All times are GMT -4. The time now is 14:32.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.