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

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

    consider something like this:

    int SumOfString(char* s)
    {
        int res = 0;
        do
        {
            res += *s;
            ++s;
        } while (*s != '\0');
    }
    

    It so happens that '\0' is 0, but I hope you get the point.

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

    It's useful for when you want to "do" something "until" a condition is satisfied.

    It can be fudged in a while loop like this:

    while(true)
    {
    
        // .... code .....
    
        if(condition_satisfied) 
            break;
    }
    
    0 讨论(0)
  • 2020-11-29 03:17

    In response to a question/comment from unknown (google) to the answer of Dan Olson:

    "do { ... } while (0) is an important construct for making macros behave well."

    #define M do { doOneThing(); doAnother(); } while (0)
    ...
    if (query) M;
    ...
    

    Do you see what happens without the do { ... } while(0)? It will always execute doAnother().

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

    do { ... } while (0) is an important construct for making macros behave well.

    Even if it's unimportant in real code (with which I don't necessarily agree), it's important for for fixing some of the deficiencies of the preprocessor.

    Edit: I ran into a situation where do/while was much cleaner today in my own code. I was making a cross-platform abstraction of the paired LL/SC instructions. These need to be used in a loop, like so:

    do
    {
      oldvalue = LL (address);
      newvalue = oldvalue + 1;
    } while (!SC (address, newvalue, oldvalue));
    

    (Experts might realize that oldvalue is unused in an SC Implementation, but it's included so that this abstraction can be emulated with CAS.)

    LL and SC are an excellent example of a situation where do/while is significantly cleaner than the equivalent while form:

    oldvalue = LL (address);
    newvalue = oldvalue + 1;
    while (!SC (address, newvalue, oldvalue))
    {
      oldvalue = LL (address);
      newvalue = oldvalue + 1;
    }
    

    For this reason I'm extremely disappointed in the fact that Google Go has opted to remove the do-while construct.

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

    The following common idiom seems very straightforward to me:

    do {
        preliminary_work();
        value = get_value();
    } while (not_valid(value));
    

    The rewrite to avoid do seems to be:

    value = make_invalid_value();
    while (not_valid(value)) {
        preliminary_work();
        value = get_value();
    }
    

    That first line is used to make sure that the test always evaluates to true the first time. In other words, the test is always superfluous the first time. If this superfluous test wasn't there, one could also omit the initial assignment. This code gives the impression that it fights itself.

    In cases such like these, the do construct is a very useful option.

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

    Well maybe this goes back a few steps, but in the case of

    do
    {
         output("enter a number");
         int x = getInput();
    
         //do stuff with input
    }while(x != 0);
    

    It would be possible, though not necessarily readable to use

    int x;
    while(x = getInput())
    {
        //do stuff with input
    }
    

    Now if you wanted to use a number other than 0 to quit the loop

    while((x = getInput()) != 4)
    {
        //do stuff with input
    }
    

    But again, there is a loss in readability, not to mention it's considered bad practice to utilize an assignment statement inside a conditional, I just wanted to point out that there are more compact ways of doing it than assigning a "reserved" value to indicate to the loop that it is the initial run through.

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