PDA

View Full Version : Loops: What they are and how to use them



ZephyrsFury
07-23-2007, 04:12 AM
Loops - What They Are And How To Use Them
By ZephyrsFury

As the title suggests, this tut is about loops. Loops can be used for main things in many different ways but the main purpose of a loops is to repeat something without actually writing it out over and over again. Now there are 3 main loops types plus one that I've recently discovered and found to be quite useful. First of all, we'll talk about the most common one:

Repeat Until Loops
You've most likely used this type of loop in a script you are making or at least come across it in your leeching... I mean downloading of script to test it but never actually giving feedback or anything. It consists of two statements, the 'repeat' statement and the 'until' statement. The until statement requires something after it to tell SCAR when to stop repeating. I'll show this in the example below. Basically, SCAR or any other PASCAL compiler will repeat whatever is between the repeat and the until statements until something becomes true. ie: Something is found or something happens.This is an example:

program New;
var c: integer;
begin
repeat
Writeln('hi');
c := c + 1;
until(c = 5)
end.

If you understood my paragraph above, you should be able guess what will happen. The script will repeat writing 'hi' in the debug until c is equal to 5. Notice how I added (c = 5) after until. This bracket (it doesn't have to be a bracket) tells the compiler when to stop. If I take away the c := c + 1 part, the loop will continue indefinately because c will never equal 5. "What's the point of this?" I hear you ask. "Why can't we just do this?: "

program New;
begin
Writeln('hi');
Writeln('hi');
Writeln('hi');
Writeln('hi');
Writeln('hi');
end.


Yes you could do that but what happens if I want you to make the script say 'hi' 50 times. It's not that fun writing it out over and over again is it. Also, what happens if I want the script to do that until it find a certain colour? You don't know how many times it will repeat itself so you can't write it out manually. Another thing I should also mention is that repeat loops will always run the what's being looped at least once.

Breaks
Even though this isn't really a loop, I think I should include it anyway as it is used quite often when dealing with loops. The 'break' statement (or procedure or whatever it is) is used to exit the loop even if the loop hasn't been finished yet. Take a look below:


program New;
var c: integer;
begin
repeat
if (c = 3) then
Break;
Writeln('hi');
c := c + 1;
until(c = 5)
end.

If you run the script, 'hi' is only displayed 3 times even though the until statement tells it to repeat 5 times. This is because I said this: 'if c is equal to 3 then Break (exit the loop)'. Although this example is quite useless, the Break statement (procedure) can be used in many ways for different things. It can also be used to break any type of loop include the ones I will talk about below. Look at one of the examples further down for a more practical use.

Continues
Like a break, this isn't really a type of loop but it can be useful when designing and using loops. Its called a 'continue'. When the 'continue' statement is called, the script goes back to the begin of the loop without finishing. I don't really know how to explain it better so I'll show you this example:


program New;
var i: integer;

begin
for i := 1 to 10 do
begin
if (i = 5) then
Continue;
Writeln(IntToStr(i));
end;
end.


Now if you run this script, it will display the numbers in your debug from 1 to 10 but it will skip 5. This is because of this line(s):

if (i = 5) then
Continue;

What happens is when i = 5, the continue command is called and the script skips past the writeln part and goes back to the beginning of the loop again starting with i = 6.

While Do Loops
As the name suggests. The While Do loop will do something while something is true. This loop consists of two statements, the 'while' statement and the 'do' statement. This means that like the repeat loop, something must be added AFTER the while statement for the loop to work. For example:


program New;
var c: integer;
begin
while (c < 5) do
begin
Writeln('hi');
c := c + 1;
end;
end.


If you run this script, it does exactly the same thing as the example of the repeat loop except it doesn't have to run through the loop at least once. If you change (c < 5) to (c < 0), nothing will happen at all. This is because c is not less than 0 so the loop will never start. Also, note that if you don't put the begin after the while do line, the script will only repeat the next line not the c := c + 1 line.

For Do Loops
Now these are quite useful for several things. I've used this loop quite often in my Rune Mysteries Runner to do several things including autocolouring. It consists of again two statements the 'for' statement and the 'do' statement. The for statement requires a variable integer that it adds to everytime the loop is repeated so that it does the same thing again but with a different integer value. Did you get any of that?... Here's the example:


program New;
var c: integer;
begin
for c := 0 to 5 do
Writeln('hi');
end.


Notice how I don't have to add c := c + 1 to the loop because the for statement automatically does that. Also note that I didn't put the begin and end statements on either side of writeln('hi'); because the for do loop repeat the line immediately below it and there's nothing else I want to put in the loop. Now before I was saying how useful this type of loop can be. Take a loop at a snippet from my Rune Mysteries Runner:


program New;

function FindDoorColour: integer;
var i, x, y, x1, y1 : integer;
begin
for i := 200 to 254 do
begin
if (FindColor(x, y, i, MMX1, MMY1, MMX2, MMY2)) then
begin
if (FindColor(x1, y1, i, x - 1, y - 1, x + 1, y - 1)) or
(FindColor(x1, y1, i, x - 1, y - 1, x - 1, y + 1)) or
(FindColor(x1, y1, i, x - 1, y + 1, x + 1, y + 1)) or
(FindColor(x1, y1, i, x + 1, y - 1, x + 1, y + 1)) then
begin
Result := i;
Break;
end;
end;
end;
end;

begin
Writeln('Door Colour = '+IntToStr(FindDoorColour));
end.


If you understood what I tried to explain above, then you would know what this (part of a) function works. It looks for a colour on the minimap starting with 200. If it finds the colour then it searches for that same colour 1 pixel surrounding the point that it was initially found. If it doesn't find the colour then it will add one to 'i' then search again until it finishes searching for colour 254 or it finds the colour that matches the criteria. If it finds the colour 1 pixel surrounding the initial point then it returns 'i' as the result of the function and breaks the loop. Try it out... go into Runescape and run the script in a place with a lot of doors and drop dots. eg Lummy Castle. It should find the colour of the red doors and ignore the colour of the drop dots then write the colour in the debug.

Calling a procedure inside the same procedure
I've just discovered this to work. I haven't used it but it could be used quite effectively. Basically make a procedure then inside that procedure you make SCAR call the same procedure... What did he say?? Have a look:


program New;
procedure Hello;
begin
Writeln('hi');
Hello;
end;

begin
Hello;
end.


If you run this script, your debug will go crazy as it is spammed with 'hi'. This example will repeat itself continuously but I can think of another more useful example:


program New;
var c: integer;
procedure Hello;
begin
Writeln('hi');
c := c + 1;
if (c < 10) then
Hello;
end;

begin
Hello;
end.

Now you can see that it will only repeat itself if c is less than 10. I suppose you could also use this for other things aswell such as repeating when a colour is found, etc.

If you have any questions or feel that I have left something out, please post here or PM me. If you can't be bothered reading but still have question then DON'T post or PM me. :p

WhiteShadow
07-23-2007, 04:27 AM
Nice&clean, cool.

bullzeye95
07-23-2007, 10:07 PM
Very nice tutorial. Might want to add continue. If you don't know what that is, check the wiki, under loops. (I just learned what they are, and they come in handy)

ZephyrsFury
07-24-2007, 07:25 AM
I've just discovered them as well. I haven't seen any in any scripts I've come across but I could see their usefulness.

EDIT: Even though I don't think many people care but I've updated the tutorial and added 'continues' as bullzeye suggested.