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

This is a discussion on Is this the most pointless code ever, or am I missing something? within the C++ Programming forums, part of the General Programming Boards category; Is there any point to doing this? Code: do { //Whatever code you want here } while (0) Correct me ...

  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
    Portugal
    Posts
    7,435
    Yes. The Do While statement there is unnecessary. The literal integer basically ruins its purpose.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,046
    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: macros [/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
    21,588
    That sounds like a really horrible hack in this day and age of templates and generic programming.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Portugal
    Posts
    7,435
    The fact we need to use macros to explain that loop is a sure sign the loop is useless.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    21,588
    This is a do while loop, not a while loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,796
    >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
    Portugal
    Posts
    7,435
    Well, I would also like to see exactly in which context that loop can be useful.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,796
    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
    Portugal
    Posts
    7,435
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

Page 1 of 2 12 LastLast
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

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