Board index » delphi » question: repeat/until

question: repeat/until

HI,
does i ever become equal to 10?. if not, how can i make
it equal to 10?.
J

j := 0;
repeat
 i := j;
 j := j + 0.5;
until j = 10;

--
Sent by  from yahoo part  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


"y_jim542...@yahoo.com" <u246063...@spawnkill.ip-mobilphone.net> schreef
in bericht news:l.1014158708.1680084228@[63.127.215.130]...

Quote
> HI,
> does i ever become equal to 10?. if not, how can i make
> it equal to 10?.
> J

> j := 0;
> repeat
>  i := j;                   use I:= round(j);  instead
>  j := j + 0.5;
> until j = 10;

Why don't you just download a pascal compiler somewhere instead of asking
stupid questions here.
You would have found out that your program does not compile, because you
are trying to assign. And you would have found the answer to your
question yourself if you did manage to get it running.

Would you please stop posting to the non existent group
"comp.lang.pascal"
--
Femme

Re:question: repeat/until


Quote
u246063...@spawnkill.ip-mobilphone.net  (y_jim542...@yahoo.com) writes:
> j := 0;
> repeat
>  i := j;
>  j := j + 0.5;
> until j = 10;
> does i ever become equal to 10?.

What are the declared types of i and j?

Quote
> if not, how can i make
> it equal to 10?.

Add a statement
  i := 10;

--
Eric

GOD is real unless declared integer.

Re:question: repeat/until


In article <l.1014158708.1680084228@[63.127.215.130]>,
 u246063...@spawnkill.ip-mobilphone.net  (y_jim542...@yahoo.com)

Quote
 wrote:
> HI,
> does i ever become equal to 10?. if not, how can i make
> it equal to 10?.
> J

> j := 0;
> repeat
>  i := j;
>  j := j + 0.5;
> until j = 10;

If j is of type real, try using "until j >= 10" instead.

If j is of type integer, " j := j + 0.5" may not change the value of
j, but it should give you some kind of error, if you have all your
error checking modes on.

Re:question: repeat/until


 j := 0;
 repeat
 j := j + 0.5;
 i := j;
 until j = 10;

"y_jim542...@yahoo.com" <u246063...@spawnkill.ip-mobilphone.net> wrote in
message news:l.1014158708.1680084228@[63.127.215.130]...

Quote
> HI,
> does i ever become equal to 10?. if not, how can i make
> it equal to 10?.
> J

> j := 0;
> repeat
>  i := j;
>  j := j + 0.5;
> until j = 10;

> --
> Sent by  from yahoo part  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


In article <hrpj8.3509$_L.2...@news1.bloor.is>,
 "John Blackwood" <jb_cornfl...@hotmail.com> wrote:

Quote
>  j := 0;
>  repeat
>  j := j + 0.5;
>  i := j;
>  until j = 10;

> "y_jim542...@yahoo.com" <u246063...@spawnkill.ip-mobilphone.net> wrote in
> message news:l.1014158708.1680084228@[63.127.215.130]...
> > HI,
> > does i ever become equal to 10?. if not, how can i make
> > it equal to 10?.
> > J

> > j := 0;
> > repeat
> >  i := j;
> >  j := j + 0.5;
> > until j = 10;

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

Since you are apparently working with j of type REAL, your last line
should read
until j >= 10;your loop may not end may exit proiperly.

Re:question: repeat/until


"Virgil" <vmh...@attbi.com> wrote

Quote
> > > does i ever become equal to 10?. if not, how can i make
> > > it equal to 10?.
> > > J

> > > j := 0;
> > > repeat
> > >  i := j;
> > >  j := j + 0.5;
> > > until j = 10;

> Since you are apparently working with j of type REAL, your last line
> should read
> until j >= 10;your loop may not end may exit proiperly.

With real numbers you have no assurance that the "final" value of
j will be exact 10.0

It may become 10.0000000001 in which case your original code
will fail to exit correctly but the above code will,

and it may become 9.999999999 in which case both codes will
fail to work as intended. (The above code will loop once to many)

The safest code is to count by integers, but if you must step a
real number then you should allow for the possible inaccurancy
and test for (say): j > 9.8

regards Sven

Re:question: repeat/until


My personal preference is to write a function, call it "IsEqualTo", and
let it do the work of comparing reals.  This allows you to "hide the
details" as you wish.  Note that there are multiple ways to define this
function -- the following is an example:

  FUNCTION IsEqualTo (a, b : real) : boolean;

   CONST
     tolerance = 1.0E-3;

   BEGIN   { IsEqualTo }
    IsEqualTo := abs(a - b) < tolerance
   END;

Now, the loop would end with

   UNTIL IsEqualTo (j, 10);

Bob Schor
Pascal Enthusiast

Quote
Sven Pran wrote:
> "Virgil" <vmh...@attbi.com> wrote

> > > > does i ever become equal to 10?. if not, how can i make
> > > > it equal to 10?.
> > > > J

> > > > j := 0;
> > > > repeat
> > > >  i := j;
> > > >  j := j + 0.5;
> > > > until j = 10;

> > Since you are apparently working with j of type REAL, your last line
> > should read
> > until j >= 10;your loop may not end may exit proiperly.

> With real numbers you have no assurance that the "final" value of
> j will be exact 10.0

> It may become 10.0000000001 in which case your original code
> will fail to exit correctly but the above code will,

> and it may become 9.999999999 in which case both codes will
> fail to work as intended. (The above code will loop once to many)

> The safest code is to count by integers, but if you must step a
> real number then you should allow for the possible inaccurancy
> and test for (say): j > 9.8

> regards Sven

Re:question: repeat/until


Quote
"Bob Schor" <bsc...@pitt.edu> wrote in message

news:3C8F5DFC.F454C523@pitt.edu...

Quote
> My personal preference is to write a function, call it "IsEqualTo", and
> let it do the work of comparing reals.  This allows you to "hide the
> details" as you wish.  Note that there are multiple ways to define this
> function -- the following is an example:

>   FUNCTION IsEqualTo (a, b : real) : boolean;

>    CONST
>      tolerance = 1.0E-3;

>    BEGIN   { IsEqualTo }
>     IsEqualTo := abs(a - b) < tolerance
>    END;

> Now, the loop would end with

>    UNTIL IsEqualTo (j, 10);

> Bob Schor
> Pascal Enthusiast

And a good example too, but I think the tolerance should
be relative, not absolute:

IsEqualTo := abs(a-b) < tolerance * abs(a);

regards Sven

Re:question: repeat/until


John Blackwood heeft geschreven in bericht ...
<snip>

Quote
>"y_jim542...@yahoo.com" <u246063...@spawnkill.ip-mobilphone.net> wrote in
>message news:l.1014158708.1680084228@[63.127.215.130]...
>> HI,
>> does i ever become equal to 10?. if not, how can i make
>> it equal to 10?.
>> J

>> j := 0;
>> repeat
>>  i := j;
>>  j := j + 0.5;
>> until j = 10;

<snip>

I think you are confusing integers with reals.
An integer can fit into a real variable, but not
the other way around.

After 20 steps <j> becomes 10.0 (real).
if you want <i> to be integer and stay that way,
maybe you should write:

(tip: use i,,j and k for integers and r1,r2, a,b,c for reals,
write real constants as 0.0 and 10.0 instead of 0 and 10,
so you will not confuse yourself or others.)

k:=0;     {new integer i}
repeat
     r1:=i/2;           {former real i}
     r2:=i/2+0.5;   {former real j}
     k:=k+1
     j:=i div 2         {new integer j}
until j>=10;

or maybe this:

for i:=0 to 20 do
begin
     r1:=i/2;
     r2:=(i/2)+1/2
end;

Huub.

Re:question: repeat/until


That's the beauty of the functional approach.  You can design the test that
is most
appropriate for the occasion!

Bob Schor

Quote
Sven Pran wrote:
> "Bob Schor" <bsc...@pitt.edu> wrote in message
> news:3C8F5DFC.F454C523@pitt.edu...
> > My personal preference is to write a function, call it "IsEqualTo", and
> > let it do the work of comparing reals.  This allows you to "hide the
> > details" as you wish.  Note that there are multiple ways to define this
> > function -- the following is an example:

> >   FUNCTION IsEqualTo (a, b : real) : boolean;

> >    CONST
> >      tolerance = 1.0E-3;

> >    BEGIN   { IsEqualTo }
> >     IsEqualTo := abs(a - b) < tolerance
> >    END;

> > Now, the loop would end with

> >    UNTIL IsEqualTo (j, 10);

> > Bob Schor
> > Pascal Enthusiast

> And a good example too, but I think the tolerance should
> be relative, not absolute:

> IsEqualTo := abs(a-b) < tolerance * abs(a);

> regards Sven

Re:question: repeat/until


Hi, I just have read some reply's to this one and I really like
some of the solves that have been suggested here.
The Equality check for reals will be very useful to me on other
projects, thanks guys.

Now I finally got to actually try the original routine below, I thought
there may have been a problem with real to integer comparison but
after trying it out I found this code will only compile without error
when both i and j are declared as reals.
Then the loop will terminate as expected with values of i=9.5 and j=10.0

I guess the answer is no i will never be 10 and i=10.0 on the second part
of the question.
Leaves me wondering what the intention of the code is suppost to do?

Quote
"John Blackwood" <jb_cornfl...@hotmail.com> wrote in message

news:hrpj8.3509$_L.2964@news1.bloor.is...
Quote
> j := 0;
>  repeat
>  j := j + 0.5;
>  i := j;
>  until j = 10;

> "y_jim542...@yahoo.com" <u246063...@spawnkill.ip-mobilphone.net> wrote in
> message news:l.1014158708.1680084228@[63.127.215.130]...
> > HI,
> > does i ever become equal to 10?. if not, how can i make
> > it equal to 10?.
> > J

> > j := 0;
> > repeat
> >  i := j;
> >  j := j + 0.5;
> > until j = 10;

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

Other Threads