Results 1 to 21 of 21

Thread: Goto / Label [Beginner - Intermediate]

  1. #1
    Join Date
    Oct 2006
    Location
    C:\Program Files\SCAR 2.03
    Posts
    1,194
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Goto / Label [Beginner - Intermediate]

    Well, recently someone had requested a Tutorial on labels, so I decided to make a Tutorial about them and how they work. This is my second tutorial although the first was a remake of someone else's tutorial about for to do statements that you can find here.

    Firstly you must know labels by themselves do not do anything other than declare an "anchor". To actually put labels to use you must use them with a little thing called goto.

    Label is a basically the same thing as var just it's used for a different purpose, it is more like an indicator that tells the script that "MyLabel" is an anchor or a marked spot in the script. You declare a label before begin and after var in a script, so it will look something like this...

    SCAR Code:
    program New;
    var
      SomeRandomVariable: string;
    label MyLabel;
    begin
    end.

    Goto is just the command that you use after you declare your label, but you must use it in such a way that the script "knows" where you want it to go to. To make an "anchor" to where you want your script to go to all you need to do is type the name of your label with a colon after it : , this tells the script where your anchor is.

    After you've set the anchor you need to tell the script to goto it right? This is basically self explanatory, but all you do is type goto and then the name of your label. Such as in the example below...

    SCAR Code:
    program New;
    var
      Hello: string;
    label MyLabel;// <- Telling the script that "MyLabel" is an anchor.
    begin
      Hello:= 'Hiya';
      MyLabel:// <- Where the script will go to when "goto" is called.
      if(Random(10) < 9)then
      begin
        Writeln(Hello);
        goto MyLabel;// <- This is telling the script to go to MyLabel.
      end;
    end.

    Very simple Huh? Well, labels and goto statements aren't all that difficult to use, but to use them correctly, is a little bit more of a challenge.

    *Warnings* While goto statements are a very useful tool for getting out of very complex nets of coding or just telling the script to repeat something or skip something if an event occurs, there are a lot of things you cannot do with goto statements, but I'll show you a quick trick to solve those problems.

    *Warning #1*


    The first problem and the most asked about one is that they're having problems using goto in a loop. For whatever reason SCAR will not allow you to do this, this means that you cannot use goto after repeat, while, and for, or in any other loop. If you try to use goto in any of those loops you will be presented with this error.

    Failed when compiling
    Line __: [Error] (10:4): Invalid jump in script

    Here are some examples of what not to do with a goto statement.

    SCAR Code:
    program New;
    label MyLabel;
    begin
      MyLabel:
      repeat
        begin
          Writeln('Random Number: ' + inttostr(Random(10)));
          if(Random(10) < 2)then goto MyLabel;
        end;
      until(False);
    end.

    SCAR Code:
    program New;
    label MyLabel;
    begin
      MyLabel:
      while (Random(10) > 1) do
        begin
          Writeln('Random Number: ' + inttostr(Random(10)));
          Wait(10);
          if(Random(10) < 2)then goto MyLabel;
        end;
    end.

    SCAR Code:
    program New;
    var
      i: Integer;
    label MyLabel;
    begin
      MyLabel:
      for i:= 0 to 10 do
        begin
          Writeln('Number: ' + inttostr(i));
          if((i + Random(5)) >= 10)then goto MyLabel;
        end;
    end.

    So here's the trick, its quite simple and easy to incorporate into your script. All you need to do is first set a Boolean that you can identify and use to tell the script that something happened and it needs to goto your label. Then when you want it to possibly goto your label you type...

    SCAR Code:
    if(SomethingHappens)then
      begin
        BooleanVar:= True;
        Break;
      end;

    The BooleanVar you set to true is the variable that will be used to tell the script that "Something" happened. Then you break out of all the loops you are currently in, and make sure you are not in any immediate loops or the trick will not work. After all the loops are broken out of you type this

    SCAR Code:
    if(BooleanVar)then goto MyLabel;

    Simple as that but very useful and very easy to incorporate into your script. Here is what it would look like all put together for those of you who are visual learners.

    SCAR Code:
    program New;
    var
      BooleanVar: Boolean;
    label MyLabel;
    begin
      MyLabel:
      repeat
        begin
          Writeln('Random Number: ' + inttostr(Random(10)));
          if(Random(10) < 2)then
          begin
            BooleanVar:= True;
            Break;
          end;
        end;
      until(False);
      if(BooleanVar)then
      begin
        Writeln('Random(10) < 2');
        goto MyLabel;
      end;
    end.

    This will make sure that you do not get the error anymore when you want to use goto inside of a loop.

    *Warning #2*

    This isn't much of a problem that is hard to fix but it is a basic warning I want to tell you so you don't crash your computer testing a script and lose all your progress.

    The problem is that when you use goto some people forget to tell it when to stop so they get into infinite loops and it lags like crazy, then they're computer crashes and they get mad. I'm going to help you prevent that in two easy lines of coding.

    Firstly declare any random Integer variable I'm going to use "Count" as my variable. Then you type in these two simple lines of coding before your goto statement or your trick goto statement *Inside the loop*.

    SCAR Code:
    Count:= Count + 1;
    if(Count > 5)then Exit else //Then your Trick "goto".

    Simple enough, but some people do forget and when their computer crashes they always blame goto but never themselves.

    This is what it should look like in a full loop like the one at the bottom of Warning #1.

    SCAR Code:
    program New;
    var
      BooleanVar: Boolean;
      Count: Integer;
    label MyLabel;
    begin
      MyLabel:
      repeat
        begin
          Writeln('Random Number: ' + inttostr(Random(10)));
          Count:= Count + 1;
          if(Count > 5)then Exit else
          if(Random(10) < 2)then
          begin
            BooleanVar:= True;
            Break;
          end;
        end;
      until(False);
      if(BooleanVar)then
      begin
        Writeln('Random(10) < 2');
        goto MyLabel;
      end;
    end.

    I hope this has taught you enough for you to use Labels and Gotos confidently and correctly.

    If you need any more info or any help with Labels or Goto statements just post here or PM me. If you see anything misspelled or incorrect in my Tutorial please tell me.

    - Special Ed
    [FONT="Garamond"][SIZE="3"]
    Yes, I am a criminal. My crime is that of curiosity. My crime is that of judging people by what they say and think, not what they look like. My crime is that of outsmarting you, something that you will never forgive me for.
    [/SIZE][/FONT][URL="http://www.villavu.com/forum/forumdisplay.php?f=125"][IMG]http://i40.tinypic.com/r1lzdv.jpg[/IMG][/URL]

  2. #2
    Join Date
    Apr 2007
    Location
    Michigan -.-
    Posts
    1,357
    Mentioned
    2 Post(s)
    Quoted
    4 Post(s)

    Default

    whoa i nvr knew that existed lol...

    nice tut man looks real cool

    EDIT: w00t first post
    METAL HEAD FOR LIFE!!!

  3. #3
    Join Date
    Dec 2006
    Location
    utah
    Posts
    1,427
    Mentioned
    2 Post(s)
    Quoted
    7 Post(s)

    Default

    Nice tut. yeah i would have to say, if you know how to use them there really really useful, i love em !!!!..
    Co Founder of https://www.tagcandy.com

  4. #4
    Join Date
    Apr 2007
    Location
    England
    Posts
    83
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Nice tutorial, but this leads me to a question:

    If I wanted to have a couple of labels, such as myLabel[0] through to myLabel[99] or something so it leads me to ask: is it possible to get a array of labels?

  5. #5
    Join Date
    Oct 2006
    Location
    finland, helsinki
    Posts
    2,501
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

    I use them pretty often! Good tutorial i liked it! i knew all about labels anyway but for those who dont its great!

    Code:
    • Narcle: I recall Jukka releasing a bunch of scripts like this before... Its how he rolls I think. rofl
    • Solarwind: Dude, you are like... t3h s3x.
    • Hy71194: JuKKa you're a machine! You released 3 scripts in 10 minutes! :O
    • benjaa: woah.... Jukka is the man Guildminer pwns all
    • NaumanAkhlaQ: And JuKKa Is my Her0!

  6. #6
    Join Date
    Oct 2006
    Location
    Ireland
    Posts
    855
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Distort View Post
    Nice tutorial, but this leads me to a question:

    If I wanted to have a couple of labels, such as myLabel[0] through to myLabel[99] or something so it leads me to ask: is it possible to get a array of labels?
    No its not possible, scar will jsut give you errors. Nice tut =]

  7. #7
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Mawh, you explained it well, you get a cookie x). Oh wait, maybe 2 cookies! Labels, are quite usefull, if you use them right x). I have recently used this in a 'randomname' function, if the 'randomname' already existed, it went to the start again, until it has a unique name. Thats where goto loops are usefull for, imo. And that repeat 'bug' is quite annoying, in delphi it is possible to use a goto in a loop. Meh, scar isn't delphi ^^. ~Raymond
    Verrekte Koekwous

  8. #8
    Join Date
    Oct 2006
    Posts
    2,297
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    woot woot!!!
    thanks (i have to call you special or edd? ) i was waiting for because i really didn't knew how to work with these, i'll read them very soon, going to print them out and reaed it while i'm auto'ing

    thanks!

    -Stijn.
    [QUOTE=Santa_Clause;277761]I love you too TSN :p[/QUOTE]
    [CENTER][URL="http://www.stats.srl-forums.com/sigs"][IMG]http://www.stats.srl-forums.com/sigs/1324.png[/IMG][/URL][/CENTER]

  9. #9
    Join Date
    Oct 2006
    Location
    C:\Program Files\SCAR 2.03
    Posts
    1,194
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Hey I'm glad you liked it, I'm not much of a writer *normally* but it seems lately that I've gotten better.

    For using an Array of label, I don't think thats possible but I've never tried, I think that would be quite useful if you could figure it out.

    If you have any questions just be sure to ask, I don't want to leave anyone confused.

    P.S. Ummm... you can call me what ever you want but most people call me Special... I think... lol
    [FONT="Garamond"][SIZE="3"]
    Yes, I am a criminal. My crime is that of curiosity. My crime is that of judging people by what they say and think, not what they look like. My crime is that of outsmarting you, something that you will never forgive me for.
    [/SIZE][/FONT][URL="http://www.villavu.com/forum/forumdisplay.php?f=125"][IMG]http://i40.tinypic.com/r1lzdv.jpg[/IMG][/URL]

  10. #10
    Join Date
    Apr 2007
    Location
    England
    Posts
    83
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

  11. #11
    Join Date
    Feb 2009
    Location
    Hungary (GMT + 1)
    Posts
    1,774
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    This is a tutorial. A very nice one. I'm not that kind of brave man but now with a bit of fear I bump this. I realy hope nobody want to kill me :S I find this realy usefull and I haven't found any other tutorial about this. I have learned label/goto from other scripts but I had an error and this tutorial helped me. (Warning #1)
    Search owns.

  12. #12
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Labels should not be used unless they are REALLY required.

  13. #13
    Join Date
    Feb 2009
    Location
    Hungary (GMT + 1)
    Posts
    1,774
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks. I haven't found any other solution to one of my problem. I use it only once. And I won't go mad and use it everywhere after you said that. I have learned something again.

  14. #14
    Join Date
    Dec 2008
    Location
    In a galaxy far, far away...
    Posts
    584
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    labels are best to use when you wanna alternate recursions or jump from block to block, its not really bad to use it though so don't strain over finding another alternate for labels even though there are



    ~NS

  15. #15
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Would you mind sharing your problem? I may find an alternative method for you .

  16. #16
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Quote Originally Posted by Nadeem View Post
    labels are best to use when you wanna alternate recursions or jump from block to block, its not really bad to use it though so don't strain over finding another alternate for labels even though there are



    ~NS
    While labels aren't really all that bad, it's just standard practice not to use them much, mostly because they can over-complicate things if you're not careful.

  17. #17
    Join Date
    Dec 2008
    Location
    In a galaxy far, far away...
    Posts
    584
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by senrath View Post
    While labels aren't really all that bad, it's just standard practice not to use them much, mostly because they can over-complicate things if you're not careful.
    I agree Thats what I meant, but you said it better



    ~NS

  18. #18
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    goto's are very bad practice and should be avoided at all cost. I repeat, at ALL costs. If your code requires a goto, then you should consider restructuring it.

    I for one am in favor of not teaching people about gotos, it is an archaic form inheritance from the days of ASM that has no place in modern code.

    I could go into the reasons why they are bad (break modularity, make code harder to read, possibility to introducing bugs you really don't want to deal with). an if statement, while statement, and switch statement are all ways to avoid using gotos and are much easier to read.

  19. #19
    Join Date
    Dec 2008
    Location
    In a galaxy far, far away...
    Posts
    584
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by boberman View Post
    goto's are very bad practice and should be avoided at all cost. I repeat, at ALL costs. If your code requires a goto, then you should consider restructuring it.

    I for one am in favor of not teaching people about gotos, it is an archaic form inheritance from the days of ASM that has no place in modern code.

    I could go into the reasons why they are bad (break modularity, make code harder to read, possibility to introducing bugs you really don't want to deal with). an if statement, while statement, and switch statement are all ways to avoid using gotos and are much easier to read.
    I think they will only be considered really bad if new scripters are trying to read that code. Gotos are not that harmful if you have good knowledge of how to control them in situations + Aren't Goto supposed to be faster in some scenarios compared to a few loop statements? Anyway, I do not think goto should be discouraged that badly, if it is so bad it wouldn't be part of a programming language besides ASM



    ~NS

  20. #20
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Quote Originally Posted by boberman View Post
    switch statement are all ways to avoid using gotos and are much easier to read
    FYI, switch/case statements are just glorified goto statements.

  21. #21
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by senrath View Post
    FYI, switch/case statements are just glorified goto statements.
    so are if statements, loops, and any branching really. the difference is that the branching is controlled and easy to see for a programmers point of view. gotos have the potential to break scoping rules, which is really bad.

    As for the "goto is faster" statement, no, it isn't. It requires the exact same amount of instructions as a test at the top of a loop would. In the situations where it would be faster (breaking out of a triply nested loop) the programmer should consider rewriting that section of code. triple nested loops are not your friend.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Labels and goto (loop) tutorial (intermediate)
    By Freddy1990 in forum Outdated Tutorials
    Replies: 17
    Last Post: 08-07-2008, 02:30 AM
  2. GoTo
    By Jackrawl in forum OSR Help
    Replies: 2
    Last Post: 12-21-2007, 07:36 PM
  3. How to use GoTo
    By Fizzi in forum OSR Help
    Replies: 6
    Last Post: 09-27-2007, 08:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •