do...while vs. repeat...until (was: Errors aren't that simple)
Karl Heuer
karl at haddock.ima.isc.com
Thu Mar 22 09:50:39 AEST 1990
(Followups to comp.lang.misc. I'm not talking about C; I'm talking about loop
constructs in general.)
In <OVB2S3Bxds13 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
>The only sense in which repeat...until is more natural than do...while is
>if you're an assembly programmer or compiler writer [because at a low level,
>both are branch-if-false].
That's one possible justification, but it certainly isn't "the only sense" in
which it's more natural.
To take a concrete example, let's ignore the existence of <ctype.h> and
suppose that we want to test whether or not a given character is a lowercase
letter. I claim that, to me, the condition "(c >= 'a' && c <= 'z')" is a
simpler expression than "(c < 'a' || c > 'z')". I immediately recognize the
idiom in the former, but the latter causes a perceptible delay as I mentally
translate the condition. (If you disagree with this example, invent another
one--unless you want to claim that there is *no* condition that is simpler
than its inverse.)
Now, my observation is that, given a condition COND which is simpler than
!COND, I am more likely to encounter the condition "do...while (!COND)" than
"do...while (COND)". In this sense, repeat...until is the more natural
construct.
This is not a proof. It's an observation, possibly biased (though I don't
think so) and subject to being contradicted by a more extensive study.
Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint
________
(Note to those who keep on beating this red herring: the difference is *not*
so significant that I'm willing to use it as an excuse to switch from C to
another language; not even to that dialect of C that uses an "until" macro.
So don't bother to point out that "the preprocessor can fix it". Do you think
you're telling me something I don't know?)
More information about the Comp.lang.c
mailing list