View Poll Results: Uhh...vote

Voters
20. You may not vote on this poll
  • do-while

    4 20.00%
  • while

    13 65.00%
  • I never use any of these

    1 5.00%
  • Who cares

    2 10.00%

do-while vs while

This is a discussion on do-while vs while within the C++ Programming forums, part of the General Programming Boards category; I think you're right. In the istream classes, the eof bit isn't set until a failed read... so you must ...

  1. #16
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I think you're right. In the istream classes, the eof bit isn't set until a failed read... so you must need a "priming" read before you can check for eof.

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    I'm in agreement with Hunter2 - in principle. First, I'm certain that Mr. Stroustrup, himself, recommends WHILE over DO...WHILE.

    The reasoning, as I recall it, is that DO...WHILE presupposes a true condition, or forces a condition to be assumed 'true' - at least once - in order to execute the body of the loop.

    Even when a DO...WHILE loop seems to make perfect sense such as prompting a user for input, or a response to a question, a WHILE loop can be easily contructed to perform the same task.

    Now, do I see this question as being "lame" or "worthless"? Not at all. There's a difference in being expedient and in being correct even when there is no apparent resultant harm to the program in choosing 'expediency' over 'correctness'.

    No one has ever 'passed by value' when 'passing by reference' would have been the 'correct' methodology, as an example? "No harm, no foul". Right? Hmmm....

    P.S. tegwin has a point. (Eibro, too. Quit typing faster than me! ) The EOF flag isn't set until the 'read' of the 'eof' char has been made. (Credit Prelude. I didn't know until I read a post from her dealing with this specific issue. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Tegwin, as you may remember, I said "Correct me if I'm wrong" That means I don't really know what I'm talking about, I'm just guessing.

    But I don't really see why do-while is anti-preferred.. I thought that to achieve the same effect with a while-loop you have to copy the contents of the loop to before the loop? And if so, doesn't that sort of make the code uglier?

    i.e.
    Code:
    char choice = 'a';
    
    //************
    do
    {
       cout << "enter q to get out of this loop.\n";
       cin >> choice;
       if(choice != 'q')
          cout << "I said Q!!!\n";
    }while(choice != 'q')
    //************
    cout << "enter q to get out of this loop.\n";
    cin >> choice;
    while(choice != 'q')
    {
       cout << "I said Q!!!\n";
       cout << "enter q to get out of this loop.\n";
       cin >> choice;
    }
    Or something like that... I can't remember, but I had a situation sort of like that, except with the contents of the loop being something like 15 lines long instead of 2.
    Last edited by Hunter2; 11-18-2002 at 07:13 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    In my experience, the do-statement is a source of errors and confusion. The reason is that its body is always executed once before the condition is evaluated. However, for the body to work correctly, something very much like the condition must hold even the first time through. More often than I would have guessed, I have found that condition not to hold as expected either when the program was first written and tested or later after the code preceding it has been modified. I also prefer the condition "up front where I can see it." Consequently, I tend to avoid do-statements.

    Bjarne Stroustrup
    Your call. I've made mine.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I see So it's not because of any inefficiency or anything? Just that people tend to be stupid and use it in the wrong place?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    No, it's because people just tend to use it. The warning seems fairly clear, but I get your meaning.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  7. #22
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    That's good to know, but the problem is, I don't get yours What's wrong with people tending to use it?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #23
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Your call, Chief. I posted a direct quote from The Man's book. If you know better, go for it!

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If you know better, go for it!
    That's the problem I don't know better, but I don't get it either
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #25
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    A program deals with "unknowns" other than what we feed it. When we begin to "assume", we 'direct' the flow of the program rather than allowing the program to handle the data passed to it.

    That's not what we're about.

    We can channel, or funnel, user input to 'fit' the program. We can't make "assumptions". A DO...WHILE loop - effectively - makes an assumption. There is no 'test'. It executes, regardless.

    That's the bottom line. I'm not saying it doesn't work, in certain instances. It just leaves the code "loose".

    We can, and should, make the code "tight". A DO...WHILE, properly written, shouldn't have a problem. But...

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  11. #26
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I see. Thanks for the explanation, I never thought of it that way
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #27
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Cool My thoughts.

    I use while loops practically all the time in my programs. There is one exception where I think do...while loops are really great—programs that ask the user if he wants it to run again. In that case it is supposed that the user wants the program to be run the first time because he opened the program. Although it wouldn't be too difficult to change that into a while loop, I still think that do...while holds its own proper place in programming. It's the right tool for some jobs, but not most.

  13. #28
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I didnt vote, they are different things where each is better in certain situations. A while loop may never execute, a do...while will always execute once. You cant really favour one over the other.

    'nuff said.
    Couldn't think of anything interesting, cool or funny - sorry.

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I agree with endo and several others, so I didn't vote either.

  15. #30
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    >You cant really favour one over the other.
    Yes, as a matter of fact, you can, which is the whole point, and the point that Mr. Stroustrup makes pretty clearly.

    "The horse is dead, Skipper. Quit beating the darned thing!"

    Okay. I'll echo Endo.

    'nuff said.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

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