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