Thread: do while false

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    do while false

    I have been tasked with revamping some old code, and there is the construct

    Code:
    do
    {
    //stuff
    }
    while(false)
    all over the place. is there any utility to this at all, or is this person simply a nutjob?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well...<//stuff> should never get executed..
    MAYBE he put it there as extra code.....i.e to add if required later ...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Within the loop, are there any things like this?
    Code:
    if ( error ) break;
    If the loop has no break statements, then it seems fairly pointless.


    Also, multi-line macros are often written as such loops, so they can be used in the context of being a single statement (note the lack of a final ; )
    Eg.
    Code:
    #define FOO(x,y) do { \
      printf("%d\n", x ); \
      printf("%d\n", y ); \
    while ( 0 )
    > Well...<//stuff> should never get executed..
    Actually, stuff gets executed exactly once - the test (which always fails) is at the end of the loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ....o..sorry....

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the difference between while and do...while is the do block is always executed at least once.

    actually googling this thread title just turned up some articles on this structure, and there are some defenses of it (he uses 1 of the example cases, but mostly it seems useless).

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by Salem View Post
    Within the loop, are there any things like this?
    Code:
    if ( error ) break;
    If the loop has no break statements, then it seems fairly pointless.


    Also, multi-line macros are often written as such loops, so they can be used in the context of being a single statement (note the lack of a final ; )
    Eg.
    Code:
    #define FOO(x,y) do { \
      printf("%d\n", x ); \
      printf("%d\n", y ); \
    while ( 0 )
    > Well...<//stuff> should never get executed..
    Actually, stuff gets executed exactly once - the test (which always fails) is at the end of the loop.
    yes, some of them have breaks; that makes sense, but it seems like an strange way to 'simplify' the flow. offloading into inline functions rather than reading through bloated 500+line blocks seems much cleaner and clearer.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by m37h0d View Post
    yes, some of them have breaks; that makes sense, but it seems like an strange way to 'simplify' the flow. offloading into inline functions rather than reading through bloated 500+line blocks seems much cleaner and clearer.
    It's a great day when you realize somebody else's code is written more poorly than you would have written it
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Hmph, I get that feeling every day
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If it's inside a #define then it's actually someone's use of the smartest way to wrap multi-line #defines. If not, then it's probably used as a glorified goto.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True or False
    By noob programmer in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:15 AM
  2. True / False
    By goran00 in forum C Programming
    Replies: 13
    Last Post: 03-14-2008, 03:26 PM
  3. False Boolean Value?
    By kwikness in forum C Programming
    Replies: 7
    Last Post: 10-09-2007, 02:34 AM
  4. if block always false.
    By entry_point in forum C++ Programming
    Replies: 6
    Last Post: 10-05-2007, 03:08 AM
  5. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 04:08 PM