Board index » delphi » question: repeat-until

question: repeat-until

the program in below prints "i is : 6" also.
i like it to stop as soon as it gets MORE than max value.
if it is equal or less to MAX value it is ok but not MORE.
can while do handle it easier?.BP.

program mypro(output);
var i,max : integer;
begin
i := 0;
max := 5;
repeat
i := i + 1;
writeln('i is : ' , i);
until i > max;
end.

--
Sent by 1 from yahoo element  from com
This is a spam protected message. Please answer with reference header.
Posted via http://www.usenet-replayer.com/cgi/content/new

 

Re:question: repeat-until


Both while and repeat can do it.

REPEAT:

program mypro(output);
var i, max : Integer;
begin
i := 1;
max := 5;
repeat
  writeln('i is : ', i);
  i := i + 1;
until i > max;
end.

WHILE:

program mypro(output);
var i, max : Integer;
begin
i := 1;
max := 5;
while i <= max do
begin
  writeln('i is : ', i);
  i := i + 1;
end;
end.

CMAD

"raoul_s2...@yahoo.com" <u239013...@spawnkill.ip-mobilphone.net> wrote in
message news:l.1023295942.1888549804@[63.127.215.130]...

Quote
> the program in below prints "i is : 6" also.
> i like it to stop as soon as it gets MORE than max value.
> if it is equal or less to MAX value it is ok but not MORE.
> can while do handle it easier?.BP.

> program mypro(output);
> var i,max : integer;
> begin
> i := 0;
> max := 5;
> repeat
> i := i + 1;
> writeln('i is : ' , i);
> until i > max;
> end.

> --
> Sent by 1 from yahoo element  from com
> This is a spam protected message. Please answer with reference header.
> Posted via http://www.usenet-replayer.com/cgi/content/new

Re:question: repeat-until


Another correction (I didn't see it at first sight) is:

program mypro(output);
var i, max : Integer;
begin
i := 0;
max := 5;
repeat
  i := i + 1;
  writeln('i is : ', i);
until i >= max;
end.

Your mistake is in the until line. You must stop the program when the 5th
time repeat runs, by saying "until i >= max" because you write the max value
and then you stop. With your old way, firs you write the max+1 value and
then repeat is passed.

CMAD

"raoul_s2...@yahoo.com" <u239013...@spawnkill.ip-mobilphone.net> wrote in
message news:l.1023295942.1888549804@[63.127.215.130]...

Quote
> the program in below prints "i is : 6" also.
> i like it to stop as soon as it gets MORE than max value.
> if it is equal or less to MAX value it is ok but not MORE.
> can while do handle it easier?.BP.

> program mypro(output);
> var i,max : integer;
> begin
> i := 0;
> max := 5;
> repeat
> i := i + 1;
> writeln('i is : ' , i);
> until i > max;
> end.

> --
> Sent by 1 from yahoo element  from com
> This is a spam protected message. Please answer with reference header.
> Posted via http://www.usenet-replayer.com/cgi/content/new

Re:question: repeat-until


On Wed, 05 Jun 2002 16:52:22 GMT,

Quote
u239013...@spawnkill.ip-mobilphone.net  (raoul_s2...@yahoo.com) wrote:
>the program in below prints "i is : 6" also.
>i like it to stop as soon as it gets MORE than max value.
>if it is equal or less to MAX value it is ok but not MORE.
>can while do handle it easier?.BP.

>program mypro(output);
>var i,max : integer;
>begin
>i := 0;
>max := 5;
>repeat
>i := i + 1;
>writeln('i is : ' , i);
>until i > max;
>end.

{ I assume you want to output 1..5. Here are 3 loops to do that.}

program mypro(output);
var i,max : integer;
begin
i := 0;
max := 5;
repeat
i := i + 1;
writeln('i is : ' , i);
until i = max;  (********************* not ">" **************)
Writeln('Repeat-Until completed. Press <Enter>'); readln;

i := 0;
While i < 5 Do
Begin
     Inc(i);
     writeln('i is : ' , i);
End;
Writeln('While loop completed. Press <Enter>'); readln;

For i := 1 to 5 Do writeln('i is : ' , i);
Writeln('For loop completed. Press <Enter>'); readln;

end.

Other Threads