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.
do-while
while
I never use any of these
Who cares
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.
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
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.
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.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; }
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.
Your call. I've made mine.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
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
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.
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
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.
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
That's the problem I don't know better, but I don't get it eitherIf you know better, go for it!
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
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
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.
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.
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.
I agree with endo and several others, so I didn't vote either.
>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