
hate the do-while loop (Re: ugly loop; hate the ! operator)
Quote:
[...]
> occasionally I find it confusing to see
> do
> {
> a large piece of code
> }
> while(some condition);
> because it can be confused with
> {
> a large piece of code
> }
> while(some condition);
Many people (me included) write it like:
do {
a large piece of code
}while(some condition);
to avoid that problem.
Quote:
> which is obviously completely different, because the while() now
> indicates an infinite loop.
Huh?? I hope you mean something else?! Why should it be infinite? And why
should any sane person want to write a infinite loop without body?
(of course, in some cases it would have useful semantics, but it would be
quite diffucult to understand)
Consider:
while(*(p++)) ;
This is perfectly legal and useful (given a string context for example)
and is not infinite (if there is a terminating zero somewhere that is ...)
A infinite loop is typically either:
for(;;) { ..... }
or
while(1) { ..... }
depending on personal preference. Usually it isn't really infinite, of course.
Quote:
> 2. If the code is executed at least once, then it should be called an
> "until" loop, not a "while" loop (IMHO, the difference between "while"
> and "until" is not merely that the conditions for iterating are
> opposites, but also that "until" implies that the code must be executed
> before checking the condition, whereas "while" does not).
Do you base that on English sentences or Pascal? English isn't my native
language, so I might be mistaken, but I see no English reason to think so.
Quote:
>Anyway, why
> should "while(x) y;" and "do y; while(x);" do different things, when they
> appear (in English) to be saying more or less the same thing?
The same could be said about many other constructions, like &&, ||, ++a vs. a++
and more...
Anders Andersson