do...while vs. repeat...until (was: Errors aren't that simple)
Trip Martin
night at pawl.rpi.edu
Fri Mar 23 07:29:15 AEST 1990
emuleomo at paul.rutgers.edu (Emuleomo) writes:
>In article <IEC2SG5xds13 at ficc.uu.net>, peter at ficc.uu.net (Peter da Silva) writes:
>What wrong with C having
>repeat <stmt> until <condition>
> I think this is preferable to
> do <stmt> while <condition>
> Why? Because using the keyword while for two different loop structures
> in a program tends to introduce subtle error that will be hard to find
> into the program. Whereas, if the keyword until was used, there will
> be no room for such errors.
> Consider the following program fragment
>etc....
>lots of code
>etc..
>do
> etc....
> while (some_func(j++)) ; /* This stmt was introduced by a maintenance */
> /* programmer */
> a += b;
>while (not_end_of_loop) ;
>etc....
>Guess what happens to the above code inadvertently??
That's why I always do the follow, even though it's not necessary:
do {
...
} while (some condition);
Again, good coding style can avoid many potential trouble spots.
As for whether do..while is better then repeat..until, I think it's purely
a religious issue. I personally don't care, as long as I can have my loops
test either at the beginning or at the end.
An interesting note that might give some perspective to this issue is how
Plus, a little-known language that I'm somewhat familiar with, handles loops.
Instead of having both while and do..while forms of loops, it uses one form
called cycle. Cycle is an infinite loop. You can then stick exit statements
anywhere in the loop (and then can be conditional, with both flavors of tests
supported). Conceptually, it's a more general way of handling loops.
--
Trip Martin
night at pawl.rpi.edu
More information about the Comp.lang.c
mailing list