Like Tree1Likes
  • 1 Post By brewbuck

do while false

This is a discussion on do while false within the C++ Programming forums, part of the General Programming Boards category; I have been tasked with revamping some old code, and there is the construct Code: do { //stuff } while(false) ...

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

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Well...<//stuff> should never get executed..
    MAYBE he put it there as extra code.....i.e to add if required later ...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ....o..sorry....
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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
    839
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    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
    CommonTater likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21