SRL Archive - General

how to make a loop?


karlkarlngng

2010-03-06 01:04:31

i got a question i know this might have been posted before but how do i make a loop that loops 10 times then stops etc and if u dont have an answer plz dont crtisize

Dgby714

2010-03-06 01:12:52

From: karlkarlngng
i got a question i know this might have been posted before but how do i make a loop that loops 10 times then stops etc and if u dont have an answer plz dont crtisize


3 ways =)


repeat
//stuff
Inc(I);
until(I >= 10);

for I := 0 to 10 do
begin
//stuff
end;

while(I <= 10) do
begin
//stuff
Inc(I);
end;

Mistran

2010-03-06 09:37:01

Just to add a bit more detail on when you should use each type of loop:


repeat
//stuff
Inc(I);
until(I >= 10);
Use this loop if you know the code in it needs to be run at least once


for I := 0 to 10 do
begin
//stuff
end;
Use this type of loop when you know how many times the code in it will be run (i.e. Your end condition is numberic rather than logical)


while(I <= 10) do
begin
//stuff
Inc(I);
end;
Use this type when the code in the loop does not necessarily need to be run

Bionicle

2010-03-06 11:29:46

Just want to add a bit and explain some things…

you need to call I as an integer. very simple.

program New;

var
I: Integer;

begin
end.

Now I automatically has the value of 0.
The procedure
Inc(I);
Is pretty much
I := I + 1;
It is just adding one onto the value of the number. Therefor every time it would go through one of the loops above, it would add the value of one onto the integer I, and when I is equal to 10, it would break out of the loop.

If you need anything else, or don't understand something, don't be afraid to post, no one will criticize :)

Dgby714

2010-03-06 11:36:28

From: bionicle1800
Just want to add a bit and explain some things…

you need to call I as an integer. very simple.

program New;

var
I: Integer;

begin
end.

Now I automatically has the value of 0.
The procedure
Inc(I);
Is pretty much
I := I + 1;
It is just adding one onto the value of the number. Therefor every time it would go through one of the loops above, it would add the value of one onto the integer I, and when I is equal to 10, it would break out of the loop.

If you need anything else, or don't understand something, don't be afraid to post, no one will criticize :)


Just wanna add one more thing =)

IncEx(I, x); //Where x is a number
comes out as
I := I + x; //Where x is a number

Boreas

2010-03-06 12:17:25

program New;
var i:integer;
begin
i := 0; //starts out as 0 by default, but just in case you use used i before...
repeat
writeln(inttostr(i));
Inc(i);
until i >= 10;
writeln('');

i := 1;
repeat
writeln(inttostr(i));
Inc(i);
until i >= 11;
writeln('');

i := 0;
repeat
writeln(inttostr(i));
Inc(i);
until i > 9;
writeln('');

i := 1;
repeat
writeln(inttostr(i));
Inc(i);
until i > 10;
writeln('');

for i := 0 to 9 do //this makes i start at 0 anyway
begin
writeln(inttostr(i));
end;
writeln('');

for i := 1 to 10 do
begin
writeln(inttostr(i));
end;
writeln('');

i := 0;
while i < 10 do
begin
writeln(inttostr(i));
Inc(i);
end;
writeln('');

i := 0;
while i <= 9 do
begin
writeln(inttostr(i));
Inc(i);
end;
writeln('');

i := 1;
while i < 11 do
begin
writeln(inttostr(i));
Inc(i);
end;
writeln('');

i := 1;
while i <= 10 do
begin
writeln(inttostr(i));
Inc(i);
end;
writeln('');

end.
These should all do something 10 times.