Thread: Is this the most pointless code ever, or am I missing something?

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Is this the most pointless code ever, or am I missing something?

    Is there any point to doing this?

    Code:
    do
    {
    //Whatever code you want here
    }
    while (0)
    Correct me if I'm horribly mistaken, but doesn't this loop do nothing? The code will run once and only once because it's a post-condition loop. Wouldn't this be exactly the same thing as just having the code within the loop by itself?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. The Do While statement there is unnecessary. The literal integer basically ruins its purpose.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    while loop correct

    Kuphryn

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There is a reason for that. If you have a #define like this:
    Code:
    #define swap(x,y) \
        do { \
            int t = x; \
            x = y; \
            y = t; \
        } while(0)
    it mandatates a semicolon at the end of the call to swap:
    Code:
    swap(x, y);
    If you just used regular { }, then you could get away with
    Code:
    swap(x, y)
    It's just to make it look more like ordinary C code.

    There was a thread about this somewhere a while ago. I can't seem to find it though. If you're interested, look through the search results for "while(0)".

    [edit] I found it: http://cboard.cprogramming.com/showthread.php?t=66510 [/edit]
    Last edited by dwks; 07-06-2006 at 12:36 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This construct is used for macros.

    EDIT: Two minutes too late.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That sounds like a really horrible hack in this day and age of templates and generic programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The fact we need to use macros to explain that loop is a sure sign the loop is useless.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    i thought that kind of loop wouldnt run unless we had something like
    Code:
    do 
    {
    //do whatever
    }while(int != 4)
    in that kind of format

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is a do while loop, not a while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The fact we need to use macros to explain that loop is a sure sign the loop is useless.
    It's impossible to say without any relevant context. I can think of several ways to justify that code by changing the context.
    My best code is written with the delete key.

  11. #11
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by Prelude
    >The fact we need to use macros to explain that loop is a sure sign the loop is useless.
    It's impossible to say without any relevant context. I can think of several ways to justify that code by changing the context.
    I'd be interested to see that actually. Honestly curious, not being snide .

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, I would also like to see exactly in which context that loop can be useful.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The macro context was already given.

    How about a text book or tutorial? Such an example would be a good way to describe the workings of loop conditions in general (with the false condition) and the do..while loop by showing how it always runs once.

    What about evolving code where it's likely that the body will become a loop but not quite yet?

    What about debugging, where only a single iteration is required?
    My best code is written with the delete key.

  14. #14
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by Prelude
    The macro context was already given.

    How about a text book or tutorial? Such an example would be a good way to describe the workings of loop conditions in general (with the false condition) and the do..while loop by showing how it always runs once.
    Good point, that would be good for a tutorial.

    Quote Originally Posted by Prelude
    What about evolving code where it's likely that the body will become a loop but not quite yet?
    Also a very good point, and thinking about it, I have done this in my own work.

    Quote Originally Posted by Prelude
    What about debugging, where only a single iteration is required?
    I'm a bit wary on this, but I profess I wouldn't think about this either. I would prefer more a simple if statement, but I can see where this has merit in certain applications. Something to think about. Thanks!

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Oh. I thought you were talking about situations in which the code would make sense.

    I'm not even sure anymore why we are discussing this... Sure, I agree with you and I can add more... however the truth of the matter is that this is the 14th post on a subject that has only one answer:

    That loop is useless. Does nothing.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM