Why use infinite loops?

前端 未结 14 1955
深忆病人
深忆病人 2021-02-08 13:34

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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 14:04

    I would consider using an infinite loop to program the guidance system of a missile.

    while ( true ) { go2Target ( ) ; }
    

    From the perspective of the missile's guidance computer, the loop once started does repeat until the end of time.

    Perhaps a purists would favor

    while ( ! blown2Bits ( ) ) { go2Target ( ) ; }
    

    but then how do you implement the blow2Bits method? what would it mean if blow2Bits returned true?

提交回复
热议问题