Is while (true) with break bad programming practice?

后端 未结 22 2281
-上瘾入骨i
-上瘾入骨i 2020-11-27 04:38

I often use this code pattern:

while(true) {

    //do something

    if() {
        break;
    }

}   

Another progr

22条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 04:44

    He is probably correct.

    Functionally the two can be identical.

    However, for readability and understanding program flow, the while(condition) is better. The break smacks more of a goto of sorts. The while (condition) is very clear on the conditions which continue the loop, etc. That doesn't mean break is wrong, just can be less readable.

提交回复
热议问题