Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!mailrus!uunet!mcsun!unido!gmdzi!wittig From: wittig@gmdzi.UUCP (Georg Wittig) Newsgroups: comp.lang.misc Subject: Re: do...while vs. repeat...until (was: Errors aren't that simple) Message-ID: <2104@gmdzi.UUCP> Date: 19 Mar 90 14:10:35 GMT References: <8192@hubcap.clemson.edu> <17000003@hpcvra.CV.HP.COM> <5819.25f7a840@vax1.tcd.ie> <16188@haddock.ima.isc.com> <2401@sunset.MATH.UCLA.EDU> Organization: GMD - The German National Research Centre for Computer Science Lines: 29 pmontgom@oak.math.ucla.edu (Peter Montgomery) writes: > Another problem with do..while is that we cannot access >variables declared inside the block in the loop test. For example, >when updating a square root by Newton's method, the code > double sqrtx, x; > sqrtx = (some approximation to sqrt(x)) > do { > register const double err = x - sqrtx*sqrtx; > sqrtx += err/(2*sqrtx); > } while (fabs(err) > 0.001*x); >is illegal. Rather, we must declare "err" in a larger scope. No, you needn't. The problem can be avoided easily: double sqrtx, x; sqrtx = (some approximation to sqrt(x)) do { register const double err = x - sqrtx*sqrtx; sqrtx += err/(2*sqrtx); if(! (fabs(err) > 0.001*x)) break; /* HA! */ } while (1); -- Georg Wittig GMD-Z1.BI P.O. Box 1240 D-5205 St. Augustin 1 (West Germany) email: wittig@gmdzi.uucp phone: (+49 2241) 14-2294 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Freedom's just another word for nothing left to lose" (Kris Kristofferson)