PDA

View Full Version : Loops.



skilld u
07-03-2008, 03:49 PM
Loops.


Contents:

Intro to loops
for to do
for downto do
repeat
while do


Intro to loops:


Loops are very useful in scripting. Loops are a piece of code that are repeated until a defined condition is met.

for to do:


I don't really know the best way to explain this but here's an example, run this and then look in the debug box:
program New;

var
i: integer;

begin
for i := 0 to 10 do //repeats what is in the loop until i = 10.
WriteLn(IntToStr(i)); //writes the value of i in the debug box.
end.

In the debug box you should see 0-10 printed out. SCAR automatically adds 1 to the variable each time it goes through the loop. Try running this:


program New;

var
i: integer;

begin
for i := 0 to 10 do //repeats what is in the loop until i = 10.
begin
WriteLn(IntToStr(i)); //writes the value of i in the debug box.
Inc(i); //adds 1 to i.
end;
end.

Now look in the debug box, it only prints out every other number because you added 2 to i each time it went through the loop.


for to do:


for downto do is pretty much the same as for to do but it goes down:


program New;

var
i: integer;

begin
for i := 10 downto 1 do
WriteLn(IntToStr(i));
end.

Run that and it will print out the numbers 10 down to 1. If you don't understand just re-read the for to do section :p

repeat:

Repeat is probably the most used and easiest loop. Repeat repeats what is in the loop until told to stop:

program New;

var
i: integer;

begin
repeat
WriteLn(IntToStr(i));
Inc(i);
until i = 10;
end.

Run that and then look in the debug box. You should see the numbers 0-9 printed out. You can also repeat forever:

program New;

var
i: integer;

begin
repeat
WriteLn(IntToStr(i));
Inc(i);
until False;
end.

Run that and you will see what I mean. It keeps printing out the value of i forever or until you press the stop button. This is known as an endless loop and are not advised to use in scripting, but if you have enough fail-safes in the rest of your script than they are fine.

while do:


while do loops are very simple:

program New;

var
i: integer;

begin
while i < 10 do
begin
WriteLn(IntToStr(i));
Inc(i);
end;
end.

Run that and it will print out the numbers 0-9. You can also make a repeat until false loop with while do loops as well:


program New;

var
i: integer;

begin
while True do
begin
WriteLn(IntToStr(i));
Inc(i);
end;
end.

Run that and it will print out the value of i forever and ever. That's pretty much all there is to know about loops :D

rep++

bullzeye95
07-03-2008, 04:12 PM
Nice and basic. I like it, good job. You should probably add for..downto..do, and continue.

skilld u
07-03-2008, 06:47 PM
thanks, I knew that I forgot one.

boberman
07-03-2008, 06:52 PM
You might want to add a short intro to what a loop actually is (code that is repeated until a condition is met) and maybe address infinite loops and how/why to avoid them.

skilld u
07-03-2008, 07:05 PM
added an intro section and a little info on endless loops.

bullzeye95
07-03-2008, 08:23 PM
I think I worded the end of my last post bad, and I forgot one thing. I meant that you should add "continue" to the tutorial. And, what I forgot to say is, you should add "break" also.

I program You
07-03-2008, 08:34 PM
Might add what Inc(I) does? i know what it does but maybe other people don't

skilld u
07-03-2008, 08:40 PM
I did say what it does.