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

后端 未结 19 2280
清歌不尽
清歌不尽 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: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().

提交回复
热议问题