SRL Archive - OSR Help

Some sort of short hand to repeat "x" times?


Mister Snow

2012-08-15 14:16:23

Like say I have something I would like to repeat no more than 15 times… or no more than 25 times.

Is there a way to just have repeat until 15 tries then terminate script?

Edit: besides of course my current method of copy+paste 15 times. =P

shstiger2009

2012-08-15 14:30:17

repeat
Writeln('test');
until (Tries >= 15)

Just make a procedure, in this case "Tries" that adds +1 every time you loop it.

Silent

2012-08-15 17:26:23

From: Mister Snow
Like say I have something I would like to repeat no more than 15 times… or no more than 25 times.

Is there a way to just have repeat until 15 tries then terminate script?

Edit: besides of course my current method of copy+paste 15 times. =P


There are three different ways you can do it. Look at the absolute beginners tutorial for even more information and explanation but here are some examples.

program Tries;

procedure TryOne;
var tries: Integer;
begin
tries:= 0;//make sure to initialize the variable just incase.
while (tries < 15) do //run until tries isn't less than 15.
begin
Writeln('Current try: '+ToStr(tries));
Inc(tries);//increase tries.
end;
end;

procedure TryTwo;
var tries: Integer;
begin
tries:= 0;//make sure to initialize the variable just incase.
repeat //actions to repeat. Checks for conditions at the end of the loop.
Writeln('Current try: '+ToStr(tries));
Inc(tries);//increase tries.
until (tries > 14); //runs until tries is greater than 14.
end;

procedure TryThree;
var
tries: Integer;//not typical name for this loop.
someCondition: Boolean;
begin
for tries:= 0 to 14 do //initializes the variable and checks for conditions
begin //runs until tries = 14., also increases at loop.
Writeln('Current try: '+ToStr(tries));
if (someCondition = true) then //if whatever you were trying to do happens
Break; // we want to break out of the loop.
end;
end;

begin
Writeln('While loop example');
TryOne;
Writeln('Repeat unti loop example');
TryTwo;
Writeln('For to do loop example');
TryThree;
end.

Toby1

2012-08-25 05:08:30


program RepeatUntilExample;
var
a,b: Integer;
begin
b := 15;
repeat
Inc(a);
Writeln('hi');
until (a = b);
end.