SRL Archive - Outdated Tutorials

How to make loops!


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

Mr. Doctor

2009-09-13 00:45:57

Helpful, nice job.

Naum

2009-09-13 13:24:48

Also add that you can make a loop using a GoTo statement.

Other than that, good tutorial! Rep+ for effort.

Daniel

2009-09-13 15:13:57

From: bionicle1800
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 stops the loop and continues from the begining


Err, no.

Continue doesn't start the loop again from the beginning, it just skips the rest of the loop and starts again from that part. Meaning, in a for to do loop, it'll still increment the variable but skip that part of the loop afterwards (yer, i'm crap at explaining, i know :p)

For example, see this:

var
i: Integer;

begin
for i := 1 to 10 do
if(i mod 2 = 0) then
Continue else
Writeln(i);
end.

That means if i is a multiple of 2, it'll skip that part of the loop and still increment the variable i.

Good tutorial overall, though ;)

Bionicle

2009-09-13 18:34:25

[QUOTE=Dan's The Man;631501]Err, no.

Continue doesn't start the loop again from the beginning, it just skips the rest of the loop and starts again from that part. Meaning, in a for to do loop, it'll still increment the variable but skip that part of the loop afterwards (yer, i'm crap at explaining, i know :p)

For example, see this:

var
i: Integer;

begin
for i := 1 to 10 do
if(i mod 2 = 0) then
Continue else
Writeln(i);
end.

That means if i is a multiple of 2, it'll skip that part of the loop and still increment the variable i.

Good tutorial overall, though ;)

oopsies :o ill fix that, thx for pointing it out :)

Smarter Child

2009-09-14 01:26:50

For this one:

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.

Do : until(looping >= 10);//It ends if looping is greater than or = to 10, just in case it goes one over, it won't keep looping forever.

For :

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.

while(n <> 10) do//this means that when n is equal to 10, the loop stops repeating

can confuse people a little, maybe re word that. such as it only loops if N isn't 10. You don't have to though.

Good Job though :) ~

ABC.def.GHI

2009-09-14 02:46:01

From: Smarter Child
For this one:

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.

Do : until(looping >= 10);//It ends if looping is greater than or = to 10, just in case it goes one over, it won't keep looping forever.

For :

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.

while(n <> 10) do//this means that when n is equal to 10, the loop stops repeating

can confuse people a little, maybe re word that. such as it only loops if N isn't 10. You don't have to though.

Good Job though :) ~


Just to let you know, with a 400ms wait, the chances of it going over 10 are about 0.. :) The only reason you'd want to do that is if it were something like..


repeat
Inc(i);
Status(i);
Disguise(GetStatus);
Writeln(GetStatus);
Random(999999999);
until(i = 999999999);

Because that could most definitely confuse the program once it does that. :)

And you could do while(n < 10)do.. that'd work just as well. :p Nothing too big though.

Daniel

2009-09-20 06:27:16

From: bionicle1800
Continue: when used, this skips the beginning of the loop and continues from the 'Continue' you placed


Noo, you still got it wrong :p (man i'm really crap at explaining things). A continue skips the rest of the loop after when the continue was called, checks the Until then goes back to the beginning :p

BobboHobbo

2009-09-20 09:56:15

[QUOTE=Dan's The Man;634306]Noo, you still got it wrong :p (man i'm really crap at explaining things). A continue skips the rest of the loop after when the continue was called, checks the Until then goes back to the beginning :p

Ill put that into begginer words xD.

Continue: Skips the rest of the current loop, then it checks the Until statement if it can break it, if not it will loop again.

There we go i think thats about right?

SCAR 3.21

2009-09-26 20:48:15

helped me, reputation :)