Is there ever a need for a “do {…} while ( )” loop?

后端 未结 19 2150
清歌不尽
清歌不尽 2020-11-29 02:44

Bjarne Stroustrup (C++ creator) once said that he avoids \"do/while\" loops, and prefers to write the code in terms of a \"while\" loop instead. [See quote below.]

S

相关标签:
19条回答
  • 2020-11-29 03:00

    (Assuming you know the difference between the both)

    Do/While is good for bootstrapping/pre-initializing code before your condition is checked and the while loop is run.

    0 讨论(0)
  • 2020-11-29 03:01

    My problem with do/while is strictly with its implementation in C. Due to the reuse of the while keyword, it often trips people up because it looks like a mistake.

    If while had been reserved for only while loops and do/while had been changed into do/until or repeat/until, I don't think the loop (which is certainly handy and the "right" way to code some loops) would cause so much trouble.

    I've ranted before about this in regards to JavaScript, which also inherited this sorry choice from C.

    0 讨论(0)
  • 2020-11-29 03:02

    I hardly ever use them simply because of the following:

    Even though the loop checks for a post-condition you still need to check for this post condition within your loop so that you don't process the post condition.

    Take the sample pseudo code:

    do {
       // get data 
       // process data
    } while (data != null);
    

    Sounds simple in theory but in real world situations it would probably turn out to look like so:

    do {
       // get data 
       if (data != null) {
           // process data
       }
    } while (data != null);
    

    The extra "if" check just isn't worth it IMO. I have found very few instances where it's more terse to do a do-while instead of a while loop. YMMV.

    0 讨论(0)
  • 2020-11-29 03:02

    A do-while loop can always be rewritten as a while loop.

    Whether to use only while loops, or while, do-while, and for-loops (or any combination thereof) depends largely on your taste for aesthetics and the conventions of the project you are working on.

    Personally, I prefer while-loops because it simplifies reasoning about loop invariants IMHO.

    As to whether there are situations where you do need do-while loops: Instead of

    do
    {
      loopBody();
    } while (condition());
    

    you can always

    loopBody();
    while(condition())
    {
      loopBody();
    }
    

    so, no, you never need to use do-while if you cannot for some reason. (Of course this example violates DRY, but it's only a proof-of-concept. In my experience there is usually a way of transforming a do-while loop to a while loop and not to violate DRY in any concrete use case.)

    "When in Rome, do as the Romans."

    BTW: The quote you are looking for is maybe this one ([1], last paragraph of section 6.3.3):

    From my experience, the do-statement is a source of error and confusion. The reason is that its body is always executed once before the condition is tested. For the correct functioning of the body, however, a similar condition to the final condition has to hold in the first run. More often than I expected I have found these conditions not to be true. This was the case both when I wrote the program in question from scratch and then tested it as well as after a change of the code. Additionally, I prefer the condition "up-front, where I can see it". I therefore tend to avoid do-statements.

    (Note: This is my translation of the German edition. If you happen to own the English edition, feel free to edit the quote to match his original wording. Unfortunately, Addison-Wesley hates Google.)

    [1] B. Stroustrup: The C++ programming language. 3rd Edition. Addison-Wessley, Reading, 1997.

    0 讨论(0)
  • 2020-11-29 03:04

    Read the Structured Program Theorem. A do{} while() can always be rewritten to while() do{}. Sequence, selection, and iteration are all that's ever needed.

    Since whatever is contained in the loop body can always be encapsulated into a routine, the dirtiness of having to use while() do{} need never get worse than

    LoopBody()
    while(cond) {
        LoopBody()
    }
    
    0 讨论(0)
  • 2020-11-29 03:06

    Yes I agree that do while loops can be rewritten to a while loop, however I disagree that always using a while loop is better. do while always get run at least once and that is a very useful property (most typical example being input checking (from keyboard))

    #include <stdio.h>
    
    int main() {
        char c;
    
        do {
            printf("enter a number");
            scanf("%c", &c);
    
        } while (c < '0' ||  c > '9'); 
    }
    

    This can of course be rewritten to a while loop, but this is usually viewed as a much more elegant solution.

    0 讨论(0)
提交回复
热议问题