Raised This Month: $32 Target: $400
 8% 

how to scripting goto and labels?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
schmurgel1983
Veteran Member
Join Date: Aug 2006
Location: Germany
Old 03-23-2011 , 18:54   how to scripting goto and labels?
Reply With Quote #1

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)
}
__________________

Working on:
nothing

Last edited by schmurgel1983; 03-23-2011 at 19:00.
schmurgel1983 is offline
ehha
SourceMod Donor
Join Date: Apr 2006
Location: Sibiu
Old 03-23-2011 , 19:18   Re: how to scripting goto and labels?
Reply With Quote #2

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.
__________________

Last edited by ehha; 03-23-2011 at 19:21.
ehha is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-23-2011 , 20:29   Re: how to scripting goto and labels?
Reply With Quote #3

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 03-23-2011 , 22:23   Re: how to scripting goto and labels?
Reply With Quote #4

Quote:
Originally Posted by Exolent[jNr] View Post
Goto's should only be used if they cure cancer.
Fixed for you.
Seta00 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 03-23-2011 , 23:03   Re: how to scripting goto and labels?
Reply With Quote #5

Quote:
Originally Posted by Seta00 View Post
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)?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
schmurgel1983
Veteran Member
Join Date: Aug 2006
Location: Germany
Old 03-24-2011 , 01:36   Re: how to scripting goto and labels?
Reply With Quote #6

ah ok i understand thank you both
__________________

Working on:
nothing
schmurgel1983 is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 03-24-2011 , 06:26   Re: how to scripting goto and labels?
Reply With Quote #7

Quote:
Originally Posted by Exolent[jNr] View Post
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.
Seta00 is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 03-24-2011 , 10:48   Re: how to scripting goto and labels?
Reply With Quote #8

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.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)
rhelgeby is offline
Send a message via MSN to rhelgeby
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:48.


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