PDA

View Full Version : Goto / Label [Beginner - Intermediate]



Special Ed
09-01-2007, 05:29 AM
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 (http://www.villavu.com/forum/showthread.php?t=15417?p=187142#post187142).

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

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

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.

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.

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.


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

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

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.

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


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.

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

gerauchert
09-01-2007, 05:35 AM
whoa i nvr knew that existed lol...:eek:

nice tut man looks real cool

EDIT: w00t first post

SKy Scripter
09-01-2007, 06:27 AM
Nice tut. yeah i would have to say, if you know how to use them there really really useful, i love em !!!!..

Distort
09-01-2007, 11:23 AM
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?

JuKKa
09-01-2007, 11:27 AM
I use them pretty often! Good tutorial i liked it! i knew all about labels anyway but for those who dont its great!

~alex~
09-01-2007, 11:31 AM
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 =]

mastaraymond
09-01-2007, 11:35 AM
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

the scar noob
09-01-2007, 11:50 AM
woot woot!!!
thanks (i have to call you special or edd? :p) 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.

Special Ed
09-01-2007, 01:56 PM
Hey I'm glad you liked it, I'm not much of a writer *normally* but it seems lately that I've gotten better. :p

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 :p

Distort
09-01-2007, 02:52 PM
Right, now to attempt classes lol ^^

Sabzi
07-24-2009, 05:42 AM
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.

Da 0wner
07-24-2009, 05:43 AM
Labels should not be used unless they are REALLY required.

Sabzi
07-24-2009, 05:46 AM
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.

Nadeem
07-24-2009, 06:06 AM
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

Da 0wner
07-24-2009, 06:08 AM
Would you mind sharing your problem? I may find an alternative method for you :).

senrath
07-24-2009, 06:35 AM
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.

Nadeem
07-24-2009, 06:41 AM
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 :p



~NS

boberman
07-24-2009, 12:20 PM
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.

Nadeem
07-24-2009, 12:25 PM
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

senrath
07-24-2009, 12:27 PM
switch statement are all ways to avoid using gotos and are much easier to read

FYI, switch/case statements are just glorified goto statements.

boberman
07-24-2009, 01:49 PM
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.