Why use infinite loops?

前端 未结 14 1921
深忆病人
深忆病人 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条回答
  • 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?

    0 讨论(0)
  • 2021-02-08 14:06

    Infinite loops are useful mostly in daemon/service processes or the main loop in a game. You can even get cute with them, eg:

    const bool heatDeathOfTheUniverse = false;
    do 
    {
         // stuff
    } while(!heatDeathOfTheUniverse);
    

    They should not be used to "wait" for things like threads as was suggested by Fry. You can use the Join method of a thread object for that.

    However, if you're in a situation where your tech lead says, "infinite loops are verboten & so are multiple method/function returns & breaks" you can also do stuff like this:

    bool done = false;
    while(!done) 
    {
        if(done = AreWeDone()) continue; // continue jumps back to start of the loop   
    } 
    

    Of course if you tech lead is forcing you to do such things you should start looking for a new job.

    For more details on the continue keyword see this MSDN article.

    0 讨论(0)
  • 2021-02-08 14:08

    Nearly all apps use an infinite Main loop. ;)

    0 讨论(0)
  • 2021-02-08 14:09

    That's an incomplete example because it can be refactored to be an end-test loop with no loss of clarity, function or performance.

    int scoped_variable;
    do {
        scoped_variable = getSomeValue();
    } while (scoped_variable != some_value);
    

    Infinite loops are most often used when the loop instance doesn't have the termination test at the top or the bottom, in the simplest case. This tends to happen when there is two parts to the loop: code that must execute each time, and code that must only execute between each iteration. This tends to happen in languages like C when doing things like reading from a file or processing a database resultset where a lot has to be done explicitly. Most languages with newer paradigms can structure such code actually into the test.

    0 讨论(0)
  • 2021-02-08 14:09

    One example is in a message pump type scenario, where you want to loop forever processing any messages that come in until told to stop. Another is if you want to take a periodic action then you might write an infinite loop with a sleep in it (though it may be better to use some form of timer for this).

    There may be some other places where the loop has to perform a certain amount of work to determine whether it should exit, and it may be cleaner just to use break when this condition is true rather than set some external flag to indicate the loop should exit.

    Generally though I think it's better practice to put your exit condition in the loop statement if possible, rather than to make it infinite and exit the loop using a break statement, because the exit condition of the loop is more obvious.

    0 讨论(0)
  • 2021-02-08 14:13
    while( 1 )
    {
        game->update();
        game->render();
    }
    

    Edit: That is, my app is fundamentally based around an infinite loop, and I can't be bothered to refactor everything just to have the aesthetic purity of always terminating by falling off the end of main().

    0 讨论(0)
提交回复
热议问题