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
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
Just to add a bit more detail on when you should use each type of loop:
Use this loop if you know the code in it needs to be run at least onceCode:repeat //stuff Inc(I); until(I >= 10);
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)Code:for I := 0 to 10 do begin //stuff end;
Use this type when the code in the loop does not necessarily need to be runCode:while(I <= 10) do begin //stuff Inc(I); end;
Just want to add a bit and explain some things...
you need to call I as an integer. very simple.
SCAR Code:program New;
var
I: Integer;
begin
end.
Now I automatically has the value of 0.
The procedure
Is pretty muchSCAR Code:Inc(I);
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.SCAR Code:I := I + 1;
If you need anything else, or don't understand something, don't be afraid to post, no one will criticize![]()
NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN
These should all do something 10 times.Code: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.
There are currently 1 users browsing this thread. (0 members and 1 guests)