hate the do-while loop (Re: ugly loop; hate the ! operator) 
Author Message
 hate the do-while loop (Re: ugly loop; hate the ! operator)

     Iu sugestis pri "dum c^iam" kaj "g^is".  Mi scivolemas (?), kiom
scias la ekziston de au' ne plac^ig^as pri:
        faro { ... } dum (...)
Mi c^iam uzas:
        dum (...) { ... }

La "faro-dum" (faru-dum) cirklo estas strangega.  G^i ne similas:
        se (...) { ... }
        por (...) { ... }       (for i := 1 to 5, estas "por", ne "dum", c^u?)
        dum (...) { ... }

Estas same c^i tial, mi ne plac^ig^as pri "ripeto-g^is" (ripetu-g^is)
cirklo en Paskalo.  Sed mi *neniam* uzis la egalaj^o en C-lingvo.

                -               -               -

     Someone suggested about:
        for (;;) { ... }        ->   for ever { ... }
        while (!...) { ... }    ->   until (...) { ... }

     I wonder how many know the existance of or don't like:
        do { ... } while (...)
I always use:
        while (...) { ... }

The do-while construct is *plain weird*.  It does not "look like":
        if (...) { ... }
        for (...) { ... }
        while (...) { ... }

It is for the same reason I don't like repeat-until in Pascal.  But I have
*never* used the equivalent in C.

                                G^ONG C^i-jau' (s-r[ic^]o) / Daniel Chung (Mr.)



Wed, 11 Oct 1995 01:38:40 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)

Quote:

>      I wonder how many know the existance of or don't like:
>    do { ... } while (...)
> I always use:
>    while (...) { ... }
> The do-while construct is *plain weird*.  It does not "look like":
>    if (...) { ... }
>    for (...) { ... }
>    while (...) { ... }

 I know the existence of and like it.
 The do-while construct is plain wonderful.
   It often allows you to bypass setting a condition above the loop,
  when that condition is modified within the loop. If I know the
  loop must execute at least once, I always choose do {...} while (...).
                               --thelma

 >                           G^ONG C^i-jau' (s-r[ic^]o) / Daniel Chung (Mr.)



Wed, 11 Oct 1995 06:51:22 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)
:

[... apparent line noise deleted ...]

:      Someone suggested about:
:       for (;;) { ... }        ->   for ever { ... }
:       while (!...) { ... }    ->   until (...) { ... }
:
:      I wonder how many know the existance of or don't like:
:       do { ... } while (...)
: I always use:
:       while (...) { ... }

That's your preference.

: The do-while construct is *plain weird*.  It does not "look like":
:       if (...) { ... }
:       for (...) { ... }
:       while (...) { ... }
:
: It is for the same reason I don't like repeat-until in Pascal.  But I have
: *never* used the equivalent in C.

True, it doesn't look like the other constructs.  Nothing says that it
has to.  I use it whenever I need a sequence of instructions to be
executed AT LEAST once.  It saves me from having to possibly use an
extra flag variable to get the sequence started.

Btw, bye having "#define until(b) while(!(b))" you can now also do
"do { ... } until (...);"

                                * - * - *

Paul-Joseph "Dragon" de Werk        |        Carpe diem.
Department of Computer Science      | "First things first, but not
California State University, Fresno |  necessarily in that order."

.signature virus erradicated by NoSigVir



Fri, 13 Oct 1995 05:05:20 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)
Argh.. well, it's not as if I _hate_ the do-while loop, it's just that
it doesn't seem to have been thought out properly. Also, I remember from
K&R 2, it was mentioned that hardly anyone uses it (I don't understand
this claim). But the truly odd thing occurs when you nest a while loop
inside a do-while loop:
  do
    while ( xxx )
      yyy;
  while ( zzz );
Of course this doesn't work.. as the end of the do-while loop is the
same as the start of the while loop.. so you need to enclose the inner
loop in {}s. No other loop has this problem where a single statement
needs to be enclosed in {}s.

Andrew Scott



Sat, 14 Oct 1995 13:40:57 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)

Quote:

> But the truly odd thing occurs when you nest a while loop
> inside a do-while loop:
>   do
>     while ( xxx )
>       yyy;
>   while ( zzz );
> Of course this doesn't work.. as the end of the do-while loop is the
> same as the start of the while loop.. so you need to enclose the inner
> loop in {}s. No other loop has this problem where a single statement
> needs to be enclosed in {}s.

"Of course"?!?  If this doesn't work, your compiler is broken.  (I was
going to say "seriously broken", but upon further reflection I'm not
sure it's all that bad since it *is* easy to fix, and the fixed version
is probably more readable to humans as well as broken compilers.)

The syntax for the do statement is:

        "do" <statement> "while" "(" <expression> ")" ";"

The nested while can not possibly be the end of the do loop because no
statement has been parsed!  A quick check of a few of the compilers I
have available shows all of them parse this correctly.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070

Life's a lot more fun when you're not responsible for your actions. -- Calvin



Sat, 14 Oct 1995 21:45:42 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)

Quote:
Andrew Scott writes:
>Argh.. well, it's not as if I _hate_ the do-while loop, it's just that
>it doesn't seem to have been thought out properly. Also, I remember from
>K&R 2, it was mentioned that hardly anyone uses it (I don't understand
>this claim). But the truly odd thing occurs when you nest a while loop
>inside a do-while loop:
>  do
>    while ( xxx )
>      yyy;
>  while ( zzz );
>Of course this doesn't work.. as the end of the do-while loop is the
>same as the start of the while loop.. so you need to enclose the inner
>loop in {}s. No other loop has this problem where a single statement
>needs to be enclosed in {}s.

This loop doesn't have that problem either, if the compiler used is
at all sane.  Check out K&R again, and try your next fragment before
posting a critique of the language.   :-)

Hint:  do <statement> while(expression);

-sue



Sun, 15 Oct 1995 10:17:38 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)

Quote:

>: The do-while construct is *plain weird*.  It does not "look like":
>:   if (...) { ... }
>:   for (...) { ... }
>:   while (...) { ... }
>True, it doesn't look like the other constructs.  Nothing says that it
>has to.  I use it whenever I need a sequence of instructions to be
>executed AT LEAST once.

I have two problems with the do{}while() construct...

1. It looks different.  You may not see the problem with that, but
   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);

   which is obviously completely different, because the while() now
   indicates an infinite loop.

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).  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?

Ian Collier



Fri, 20 Oct 1995 00:14:56 GMT  
 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



Fri, 27 Oct 1995 23:16:21 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)

Quote:

>I have two problems with the do{}while() construct...

>1. It looks different.  You may not see the problem with that, but
>   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);

This is easily solved by using a couple of simple style rules:

  1. Always, always, ALWAYS put white space between adjacent
     loop constructs.  Always.

  2. Whenever you've got a large piece of code (particularly if
     it's inside a control flow construct), you should seriously
     consider putting it in its own function.

  3. Whenever you create a while with no body, it's a good idea
     to make that clear, either with a comment, or making the body
     consist of a "continue" statement.

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).

This is precisely how C's "while loop" behaves.  The "do/while", of course,
is completely different.

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?

A good general rule of programming is

  Don't let your understanding of English (or any other "human" language)
  overly effect your understanding of "computer" languages.  Computer
  languages, despite sharing a vague similarity to human languages, are
  quite different in nature.  Computer languages are made of (supposedly,
  anyway) precisely defined sentences with precisely defined semantics.
  In pretty much direct opposition, human languages have no fixed (or
  even fully definable at any one time) syntax, and the semantics is rife
  with ambiguities and unstated assumptions.

--
BLAZEMONGER -- if you don't SHOUT when you say it, you SUCK when you play it.



Wed, 01 Nov 1995 01:02:17 GMT  
 hate the do-while loop (Re: ugly loop; hate the ! operator)
BLAZEMONGER said:

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?

     Maybe this is one of the reasons why I hate the do-while loop.
How would you phrase it in French?  Esperanto?

                                                Daniel Chung (Mr.)



Wed, 01 Nov 1995 10:37:39 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. doing a loop with an array - Windows is hating my guts this week

2. hate the ! operator

3. ugly loop

4. For loops into for loops

5. Loop or Loops in "C"

6. (Newbie) My Loop Isn't Looping - Aargh!

7. SEMA - WHILE loop in a FOR loop !

8. For loops into for loops

9. wanted - C and/or Fortran source for 24-loop Livermore Loops

10. Hate new "tabbed" code document view

11. IntelliSense hates namespaces

12. So you hate C++....

 

 
Powered by phpBB® Forum Software