Why use infinite loops?

前端 未结 14 1934
轮回少年
轮回少年 2021-02-08 13:33

Another poster asked about preferred syntax for infinite loops.

A follow-up question: Why do you use infinite loops in your code? I typically see a construct like

14条回答
  •  隐瞒了意图╮
    2021-02-08 13:54

    A loop like:

    while (true)
    {
        // do something
        if (something else) break;
        // do more
    }
    

    lets you break out of the loop in the middle, rather than at the start (while/for) or end (do-while).

    If you've got a complex condition, you might also want to use this style to make the code clearer.

提交回复
热议问题