Negaal
02-17-2008, 10:01 PM
Hello, again.
This tutorial teaches you all pascal loops(what are used in scar).
So there is basically 3 of them.
They are used to repeat things until condition is met.
*Note this
"Counter := Counter + 1;"This adds every time 1 to counter when it performs the action.
First one, most common, most logical looking.
program New;
var
counter : integer;
begin
repeat
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
until(Counter >= 5)
Writeln('We got past from until, script ended.');
end.
See, we repeat the stuff what between "repeat and "until" until condition is met.
Note that codition next to "until" can only be boolean. This means it must return true or false.
Also you don't have to use only one condition. You can use "or", "and" and such.
See example.
program New;
var
counter, timer : integer;
begin
Timer := GetSystemTime;
repeat
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
until(Counter >= 5) or ((GetSystemTime - Timer) > 10000)
Writeln('We got past from until, script ended.');
end.
Instead "or" can be "and" aswell.
In this example it perfoms whats between "repeat" and "until" until counter is equal or bigger than 5, or time from entering loop is more than 10 seconds.
Also placing condition next to "until" is not only way how to get out of loop.
We can use "break".
"if Condition then break;"
Also if you don't to break out from loop then it's okay.
Then you need to use "until (false);".
program New;
var
counter, timer : integer;
begin
Timer := GetSystemTime;
repeat
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
if (Counter >= 5) or ((GetSystemTime - Timer) > 10000) then break;
until(false);
Writeln('We got breaked out from loop, script ended.');
end.
If you will remove "if (Counter >= 5) or ((GetSystemTime - Timer) > 10000) then break;" then
It repeats it forever because you got "until false", what gives you an endless loop.
Now, second, not so commonly used loop statement.
"while condition do ..."
This means, while condition returns true then do..
program New;
var
counter : integer;
begin
while Counter < 5 do
begin
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
end;
Writeln('Counter is not smaller than 5 anymore, script ended.');
end.
I think script spoke for himself, if any question then just reply.
You can use "break" here aswell.
Now the third, a very useful statement.
Example:
"for 0 to 2 do".
It performs action whats next to "do" 3 times 0, 1, 2.
Simple? Yeah!:rolleyes:
We can give integer value, in which loop it is.
"for i:=0 to 2 do".
program New;
var
counter : integer;
begin
for counter:=0 to 2 do
begin
writeln('Counter = '+IntToStr(counter));
wait(500);
end;
end.
Got it?
Counter is equal to number, how many times has it performed stuff next to "do".
First time it is 0, second time it's 1, third time 2.
Also you can count to down.
program New;
var
counter : integer;
begin
for counter:=2 downto 0 do
begin
writeln('Counter = '+IntToStr(counter));
wait(500);
end;
end.
Also there is nothing wrong when you do "Counter:=1" instead of "Counter:=0".
But we do "Counter:=0" because arrays start from 0.
If you don't know what arrays are then I suggest you to read some another tutorial about it.
Also "break;" can used here aswell.
program New;
var
i : integer;
SomeArray : Array [0..2] of integer;
begin
SomeArray[0] := 123;
SomeArray[1] := 456;
SomeArray[2] := 789;
For I := 0 to 2 do
begin
Writeln('SomeArray['+IntToStr(i)+'] := '+IntToStr(SomeArray[i])+';');
wait(500);
end;
end.
Edit:
Also if you dont know arrays length, or highest index number, or it can't be set at beginning at script you can do:
"High(x);" - Returns highest index.
or
"Length(x) - 1" Returns arrays length, but since arrays start from 0, we have to do "-1".
Example how to use them:
program New;
var
i, L : integer;
SomeArray : Array [0..2] of integer;
begin
SomeArray[0] := 123;
SomeArray[1] := 456;
SomeArray[2] := 789;
L := High(SomeArray);
For I := 0 to L do
begin
Writeln('SomeArray['+IntToStr(i)+'] := '+IntToStr(SomeArray[i])+';');
wait(500);
end;
end.
Note that we stored highest in "L", so we don't have to do
"For I := 0 to High(SomeArray) do" or "For I := 0 to (Length(SomeArray) - 1) do" every time, because they take more time than storing it in variable only once.
So thats all about loops in Scar you need to know.
Have fun;).
This tutorial teaches you all pascal loops(what are used in scar).
So there is basically 3 of them.
They are used to repeat things until condition is met.
*Note this
"Counter := Counter + 1;"This adds every time 1 to counter when it performs the action.
First one, most common, most logical looking.
program New;
var
counter : integer;
begin
repeat
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
until(Counter >= 5)
Writeln('We got past from until, script ended.');
end.
See, we repeat the stuff what between "repeat and "until" until condition is met.
Note that codition next to "until" can only be boolean. This means it must return true or false.
Also you don't have to use only one condition. You can use "or", "and" and such.
See example.
program New;
var
counter, timer : integer;
begin
Timer := GetSystemTime;
repeat
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
until(Counter >= 5) or ((GetSystemTime - Timer) > 10000)
Writeln('We got past from until, script ended.');
end.
Instead "or" can be "and" aswell.
In this example it perfoms whats between "repeat" and "until" until counter is equal or bigger than 5, or time from entering loop is more than 10 seconds.
Also placing condition next to "until" is not only way how to get out of loop.
We can use "break".
"if Condition then break;"
Also if you don't to break out from loop then it's okay.
Then you need to use "until (false);".
program New;
var
counter, timer : integer;
begin
Timer := GetSystemTime;
repeat
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
if (Counter >= 5) or ((GetSystemTime - Timer) > 10000) then break;
until(false);
Writeln('We got breaked out from loop, script ended.');
end.
If you will remove "if (Counter >= 5) or ((GetSystemTime - Timer) > 10000) then break;" then
It repeats it forever because you got "until false", what gives you an endless loop.
Now, second, not so commonly used loop statement.
"while condition do ..."
This means, while condition returns true then do..
program New;
var
counter : integer;
begin
while Counter < 5 do
begin
Counter := Counter + 1;
wait(500);
Writeln('Counter = '+IntToStr(Counter));
end;
Writeln('Counter is not smaller than 5 anymore, script ended.');
end.
I think script spoke for himself, if any question then just reply.
You can use "break" here aswell.
Now the third, a very useful statement.
Example:
"for 0 to 2 do".
It performs action whats next to "do" 3 times 0, 1, 2.
Simple? Yeah!:rolleyes:
We can give integer value, in which loop it is.
"for i:=0 to 2 do".
program New;
var
counter : integer;
begin
for counter:=0 to 2 do
begin
writeln('Counter = '+IntToStr(counter));
wait(500);
end;
end.
Got it?
Counter is equal to number, how many times has it performed stuff next to "do".
First time it is 0, second time it's 1, third time 2.
Also you can count to down.
program New;
var
counter : integer;
begin
for counter:=2 downto 0 do
begin
writeln('Counter = '+IntToStr(counter));
wait(500);
end;
end.
Also there is nothing wrong when you do "Counter:=1" instead of "Counter:=0".
But we do "Counter:=0" because arrays start from 0.
If you don't know what arrays are then I suggest you to read some another tutorial about it.
Also "break;" can used here aswell.
program New;
var
i : integer;
SomeArray : Array [0..2] of integer;
begin
SomeArray[0] := 123;
SomeArray[1] := 456;
SomeArray[2] := 789;
For I := 0 to 2 do
begin
Writeln('SomeArray['+IntToStr(i)+'] := '+IntToStr(SomeArray[i])+';');
wait(500);
end;
end.
Edit:
Also if you dont know arrays length, or highest index number, or it can't be set at beginning at script you can do:
"High(x);" - Returns highest index.
or
"Length(x) - 1" Returns arrays length, but since arrays start from 0, we have to do "-1".
Example how to use them:
program New;
var
i, L : integer;
SomeArray : Array [0..2] of integer;
begin
SomeArray[0] := 123;
SomeArray[1] := 456;
SomeArray[2] := 789;
L := High(SomeArray);
For I := 0 to L do
begin
Writeln('SomeArray['+IntToStr(i)+'] := '+IntToStr(SomeArray[i])+';');
wait(500);
end;
end.
Note that we stored highest in "L", so we don't have to do
"For I := 0 to High(SomeArray) do" or "For I := 0 to (Length(SomeArray) - 1) do" every time, because they take more time than storing it in variable only once.
So thats all about loops in Scar you need to know.
Have fun;).