在看<< the art of readable code >>, 作者提到要 避免do/while循环:
What’s weird about a do/while loop is that a block of code may or may not be reexecuted based on a condition underneath it. Typically, logical conditions are above the code they guard—this is the way it works with if, while, and for statements. Because you typically read code from top to bottom, this makes do/while a bit unnatural. Many readers end up reading the code twice.
但do/while有时候也很适用一些场景, 比如前两天 我在一个问题里回答:
do{
result = msgrcv(id, (void *) &msg, sizeof(msg.text),
msgtyp, MSG_NOERROR);
} while(result == -1 && errno == EINTR);
我们至少需要做一次msgrcv, 然后判断出错情况 是否需要循环. 这里do/while 就非常适合. 问问大家的看法.
存在即合理,虽然一般情况下用到
do/while
循环的时候很少,但有时候用用也无妨。其实
do/while
完全可以由while/do
来代替,当然得配合定义function
才会比较简捷,比如可以换成
然后被一些懒牛改写成
相比之下
No.2
烦琐,No.3
晦涩,还是No.1
好懂些。当然也有人会写成
一样晦涩,还多写几个字。
如果说
do {... }
中的语句比较多的情况……还是老办法,封装成function
就能解决,一目了然。要是觉得不清楚函数在干啥,还可以在语句块里写句注释。当然,也有人不喜欢用
while
循环的,所以就用for
循环解决了或者更 ZUO 的
当然,
while/do
或者do/while
也可以写成死循环由条件判断break
这种形式。总的来说,语法提供了,逻辑没得错误,自己写着舒服,大家都看得懂——那就用呗!