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

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

提交回复
热议问题