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

后端 未结 19 2292
清歌不尽
清歌不尽 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.

提交回复
热议问题