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

后端 未结 19 2153
清歌不尽
清歌不尽 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:06

    This is cleanest alternative to do-while that I have seen. It is the idiom recommended for Python which does not have a do-while loop.

    One caveat is that you can not have a continue in the <setup code> since it would jump the break condition, but none of the examples that show the benefits of the do-while need a continue before the condition.

    while (true) {
           <setup code>
           if (!condition) break;
           <loop body>
    }
    

    Here it is applied to some of the best examples of the do-while loops above.

    while (true);  {
        printf("enter a number");
        scanf("%c", &c);
        if (!(c < '0' ||  c > '9')) break;
    } 
    

    This next example is a case where the structure is more readable than a do-while since the condition is kept near the top as //get data is usually short yet the //process data portion may be lengthy.

    while (true);  {
        // get data 
        if (data == null) break;
        // process data
        // process it some more
        // have a lot of cases etc.
        // wow, we're almost done.
        // oops, just one more thing.
    } 
    
    0 讨论(0)
  • 2020-11-29 03:08

    It's all about readability.
    More readable code leads to less headache in code maintenance, and better collaboration.
    Other considerations (such as optimization) are, by far, less important in most cases.
    I'll elaborate, since I got a comment here:
    If you have a code snippet A that uses do { ... } while(), and it's more readable than its while() {...} equivalent B, then I'd vote for A. If you prefer B, since you see the loop condition "up front", and you think it's more readable (and thus maintainable, etc.) - then go right ahead, use B.
    My point is: use the code that is more readable to your eyes (and to your colleagues'). The choice is subjective, of course.

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

    I like David Božjak's example. To play devil's advocate, however, I feel that you can always factor out the code that you want to run at least once into a separate function, achieving perhaps the most readable solution. For example:

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

    could become this:

    int main() {
        char c = askForCharacter();
        while (c < '0' ||  c > '9') {
            c = askForCharacter();
        }
    }
    
    char askForCharacter() {
        char c;
        printf("enter a number");
        scanf("%c", &c);
        return c;
    }
    

    (pardon any incorrect syntax; I'm not a C programmer)

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

    do-while is a loop with a post-condition. You need it in cases when the loop body is to be executed at least once. This is necessary for code which needs some action before the loop condition can be sensibly evaluated. With while loop you would have to call the initialization code from two sites, with do-while you can only call it from one site.

    Another example is when you already have a valid object when the first iteration is to be started, so you don't want to execute anything (loop condition evaluation included) before the first iteration starts. An example is with FindFirstFile/FindNextFile Win32 functions: you call FindFirstFile which either returns an error or a search handle to the first file, then you call FindNextFile until it returns an error.

    Pseudocode:

    Handle handle;
    Params params;
    if( ( handle = FindFirstFile( params ) ) != Error ) {
       do {
          process( params ); //process found file
       } while( ( handle = FindNextFile( params ) ) != Error ) );
    }
    
    0 讨论(0)
  • 2020-11-29 03:13

    In our coding conventions

    • if / while / ... conditions don't have side effects and
    • varibles must be initialized.

    So we have almost never a do {} while(xx) Because:

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

    is rewritten in:

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

    and

    Handle handle;
    Params params;
    if( ( handle = FindFirstFile( params ) ) != Error ) {
       do {
          process( params ); //process found file
       } while( ( handle = FindNextFile( params ) ) != Error ) );
    }
    

    is rewritten in:

    Params params(xxx);
    Handle handle = FindFirstFile( params );
    while( handle!=Error ) {
        process( params ); //process found file
        handle = FindNextFile( params );
    }
    
    0 讨论(0)
  • 2020-11-29 03:13

    It is only personal choice in my opinion.

    Most of the time, you can find a way to rewrite a do ... while loop to a while loop; but not necessarily always. Also it might make more logical sense to write a do while loop sometimes to fit the context you are in.

    If you look above, the reply from TimW, it speaks for itself. The second one with Handle, especially is more messy in my opinion.

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