This loop will definitely terminate



  • I've seen plenty of while (true) { ... } loops which run until some condition in the body of the loop triggers a break statement. Of course, if you forget to break out of the loop, it will run forever. This programmer came up with a clever solution to this problem:

    do {
        if (p == null)
            break;
    
        // do stuff
    } while (false);
    


  • that looks like it's from an early version of C that didn't have the != operator.

    Now obviously you would just do

    if(p!=null){

    // do stuff

    }

     

    but you couldn't do that then



  • @campkev said:

    that looks like it's from an early version of C that didn't have the != operator.

    Now obviously you would just do

    if(p!=null){

    // do stuff

    }

     

    but you couldn't do that then

     

     I think you missed the point :   while(false) <===



  • @amischiefr said:

    @campkev said:

    that looks like it's from an early version of C that didn't have the != operator.

    Now obviously you would just do

    if(p!=null){

    // do stuff

    }

     

    but you couldn't do that then

     

     I think you missed the point :   while(false) <===

    And you missed the fact that a do-while loop will always run at least once.



  • Re: Community Server blows goats

    @amischiefr said:

     I think you missed the point
    Nope, he definitely got the point, and responded with a sarcastic remark about why someone would do such a boneheaded mindfscking thing. That being said, a loop that doesn't is no more WTFy than an infinite loop or a for-case. It's old news.

    Therefore, this thread is now about Community Server bugs!

    [edit: 100 GET]



  • @campkev said:

    that looks like it's from an early version of C that didn't have the != operator.

    Now obviously you would just do

    if(p!=null){

    // do stuff

    }

     

    but you couldn't do that then

    Sarcasm aside, I think C didn't have true/false until later. Basically 0 = false, anything else = true.


  • I would hope that there is a 'break' or 'continue' somewhere in // do stuff that makes that code gyration at least a little justified.  Of course, it's entirely possible that there could have been such code there originally, but it's since been removed.

    For what it's worth, one can get that same effect a little less awkwardly via:

    while (!(p == null)) { /* compatibility with mythic C compilers that don't support the != operator */
      // do stuff
      break;
    }

    (Not, of course, that the above is much better - but it bleeds a little less, and may be the best generic answer one can do without seeing what '// do stuff' entails.  <font size=-1>Other than, of course,

    while (p) {
      // do stuff
      break;
    }
    </font>

    Please remember, as this is in parentheses, normal people can't see it.)



  •  

    @Adriano said:

     And you missed the fact that a do-while loop will always run at least once.

     

    No, no I didn't.  I promise you that.  The assumption about what "p" actually is would be silly.  For all you know "p" is an object in Java, who knows.  I'm pretty sure the OP was trying to say that the creator of the code used a "break" statement in a silly way by having "while(false)" instead of "while(true)" and breaking somewhere in the code.  Hence breaking out of the loop after it runs only once, defeating the purpose of the loop.  



  •  This is rather standard C when used in macros. Either this was a macro definition and the OP decided not to include it, or it was pulled from an existing macro which would likely be a WTF.



  • @tgape said:

    I would hope that there is a 'break' or 'continue' somewhere in // do stuff that makes that code gyration at least a little justified.
    Just yesterday, I used that idiom:

    do{
        // do some stuff
        if( /* can't go on */ ) break;
    
    // do some more stuff
    if( /* can't go on */ ) break;
    
    // et cetera...
    

    } while(false);

    I felt a bit bad for writing such smelly code, but it looked better than nested ifs, I think.



  • Wouldn't a goto be clearer?



  •  A goto could go anywhere in that function.  A break will only jump out of the current do/while block, and is guaranteed to go forward.

     The fake loop makes life easier if you have multiple levels and need to rearrange code.  If you are naming the targets things like label1: and label2: then the gotos will NOT be clearer, so it requires some creativity to make them so.

     Mostly, the fake loop is used to get around the prejudice against gotos.  And yes, we do it all the time at my workplace.

     




  • @campkev said:

    that looks like it's from an early version of C that didn't have the != operator.

    Now obviously you would just do

    if(p!=null){

    // do stuff

    }

    Sorry, that won't work in newer versions of C either. They removed the null keyword from C at the same time as the != operator was introduced.



  • @zmafoox said:

     This is rather standard C when used in macros. Either this was a macro definition and the OP decided not to include it, or it was pulled from an existing macro which would likely be a WTF.

    It's actually Java code, but I believe the author was more fluent in C++ than Java, so perhaps he carried the technique over, although of course Java doesn't have macros.



  • @Emphyrio said:

    It's actually Java code, but I believe the author was more fluent in C++ than Java, so perhaps he carried the technique over, although of course Java doesn't have macros.
     

    I thought it might be a excusable code in a non-OOP language, but don't we have Exceptions for this?



  • @danixdefcon5 said:

    Sarcasm aside, I think C didn't have true/false until later. Basically 0 = false, anything else = true.

     

    Assuming sarcasm is truly aside here, C still doesn't have true/false and 0 is false/anything else true is used.  C++ has true/false, but the original 0 is false paradigm still exists.



  • Took me a couple of moments to see how this is actually a stealth forwards-only goto.  The same effect can be achieved by moving the loop contents into their own function and using 'return'.  I suppose the 'break' method is middle ground: it keeps the flow of the program intact (unlike a function call) but only allows a jump forwards to a clearly defined point, so it's more orderly than a goto (and avoids the creation of noodles - uh, I mean spagetti code).  I suggest that when using this method,  a comment such as /this is not intended to loop/ is used so that less-informed programmers don't try to 'fix' the loop.

    On the other hand, this is similar enough to a goto that it may promote raptor attacks.



  • Maybe they have a variable named 'false'.



  • @Morbii said:

    @danixdefcon5 said:

    Sarcasm aside, I think C didn't have true/false until later. Basically 0 = false, anything else = true.

     

    Assuming sarcasm is truly aside here, C still doesn't have true/false and 0 is false/anything else true is used.  C++ has true/false, but the original 0 is false paradigm still exists.

     

     

    The C99 standard provides an `#include <stdbool.h>` header which defines the `bool` type, plus `true`/`false` constants.



  • To SuperousOxide: what greydude said, and the simple fact that not all languages have goto available (I'm currently on PHP). 

    To dtech: exceptions are overkill in this situation.



  • @SpComb said:

    The C99 standard provides an #include &lt;stdbool.h&gt; header which defines the bool type, plus true/false constants.

    And "#define true (2+2<5)" should work on any C compiler.



  • @dtech said:

    @Emphyrio said:

    It's actually Java code, but I believe the author was more fluent in C++ than Java, so perhaps he carried the technique over, although of course Java doesn't have macros.
     

    I thought it might be a excusable code in a non-OOP language, but don't we have Exceptions for this?

    Exceptions aren't always an OOP feature.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.