Bionicle
2009-09-13 00:22:11
Welcome!
This script is for the beginners, on how to make a loop! This is a very useful tool, because if, per say, you want your script to preform an action multiple times, instead of making multiple sections, you just need to do one simple thing…create a loop!
Now there are three different types of loops. Repeat-Until, For-To-Do, and While-Do.
Repeat-Until
Lets start with the easiest of all. Here is the basic outline of this loop:
Here is an example of a script that will not go on forever,
For-To-Do
Now lets try the hard loop. here is the basic layout of this loop:
For-To-Do loops add one to the starting integer every time the loop restarts. Now lets look at another example:
and there you have it, that is how you do For-To-Do loops. :D
While-Do
these ones are, in my opinion, the most confusing to understand, so bear with me if you don't get it :)
heres an example:
yea, yea, i know. confusing. heres an example to help you get it better:
A few helpful tools within loops
Exit: when used, this exits the procedure or function it is placed in
Break: when used, this exits the loop it is placed in
Continue: when used, this skips the rest of the current loop, then it checks the Until statement if it can break it, if not it will loop again.
and that's it ladies and gentlemen…my first tut :) i hope this wasn't too confusing, and that you got through it ok, please post what you think needs improving and i'll try to fix it for you ;) and if you like my tut, use the blue check mark at the top corner of this box ;)!
~bionicle~
This script is for the beginners, on how to make a loop! This is a very useful tool, because if, per say, you want your script to preform an action multiple times, instead of making multiple sections, you just need to do one simple thing…create a loop!
Now there are three different types of loops. Repeat-Until, For-To-Do, and While-Do.
Repeat-Until
Lets start with the easiest of all. Here is the basic outline of this loop:
begin
repeat //the repeat is what cuts this section of the rest off the script to repeat.
--what you want repeated--;
wait(400);//IT IS IMPORTANT THAT YOU PUT IN A 'WAIT(#)' IN AN INFINITE LOOP OTHERWISE YOUR COMPUTER WILL HAVE SERIOUS LAG AND MAY EVEN CRASH
until(false);//the until(false) is what signals this is the end of the repeat. the (false) means that this particular script will go on forever.
end.Here is an example of a script that will not go on forever,
program RepeatUntilLoopExample;
var
looping: Integer;//this declares the variable looping as an integer(AKA a whole number)
begin
repeat
Inc(looping);//this takes the integer looping and adds one to it.
WriteLn('This is loop number' + IntToStr(looping) + '.');//IntToStr is short for Integer to String. this changes the variable looping into a string. WriteLn writes the line in the debug box :P
wait(400);//again, you need to wait a little before the loop restarts.
until(looping = 10);//this says that once the variable looping is equal to 10 it will stop repeating.
end.For-To-Do
Now lets try the hard loop. here is the basic layout of this loop:
for (Starting Integer) to (Ending Integer) do
begin
--what you want repeated--;
wait(400);
end.For-To-Do loops add one to the starting integer every time the loop restarts. Now lets look at another example:
program ForToDoLoopExample;
var
n: integer;
begin
for n := 0 to 10 do//n := 0 just means that n is equal to 0 when this script starts, the 10 means that once the loop repeats 10 times, it will stop repeating.
begin
WriteLn('This is loop number' + IntToStr(n) + '.');
wait(400);
end;
end.and there you have it, that is how you do For-To-Do loops. :D
While-Do
these ones are, in my opinion, the most confusing to understand, so bear with me if you don't get it :)
heres an example:
while(put the opposite what you want) do//e.g. instead of n = 10 (n is equal to 10) put n <> 10 (n is not equal to 10) if you don't get this, don't worry, you'll get it when you read the example.
begin
--what you want repeated--;
wait(400);
end.yea, yea, i know. confusing. heres an example to help you get it better:
program WhileDoLoopExample;
var
n: integer;
begin
while(n <> 10) do//this means that when n is equal to 10, the loop stops repeating
begin
Inc(n);//you remember this, eh?
WriteLn('This is loop number' + IntToStr(n) + '.';
wait(400);
end;
end.A few helpful tools within loops
Exit: when used, this exits the procedure or function it is placed in
Break: when used, this exits the loop it is placed in
Continue: when used, this skips the rest of the current loop, then it checks the Until statement if it can break it, if not it will loop again.
and that's it ladies and gentlemen…my first tut :) i hope this wasn't too confusing, and that you got through it ok, please post what you think needs improving and i'll try to fix it for you ;) and if you like my tut, use the blue check mark at the top corner of this box ;)!
~bionicle~