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++
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++