I don't get it.....
Code:#include <iostream.h>
int main()
{
int x = 0, y = 0;
while(x < 10)
{
x++;
cout << x << " ";
}
cout << endl;
do
{
y++;
cout << y << " ";
}
while(y < 10);
return 0;
}
Printable View
I don't get it.....
Code:#include <iostream.h>
int main()
{
int x = 0, y = 0;
while(x < 10)
{
x++;
cout << x << " ";
}
cout << endl;
do
{
y++;
cout << y << " ";
}
while(y < 10);
return 0;
}
the only difference is that a do-while loop executes the loop at least ONCE, before even checking if the condition is true or false. a while loop will not loop at all unless the condition is true.
Under most circumstances you can use them interchangeably, but there are some instances when a do-while is necessary over a while.
CaptainPenguin-but there are some instances when a do-while is necessary over a while.
and vise versu... It depends on what kind of flow you want in your program.
A 'while' loop will first check the condition (x < 10), and then execute the block. A 'do while' loop will first run the block, and then test the condition
So you've got the subtle difference of: "test-execute" / "execute-test"
I personaly choose "do while". But as mentioned earlier, It depends on what type of flow you want
you need a choice for both. I use both depending on what I need to get done.
It's not which one to choose....
Choose the one you need.
Use the do while loop when you want your statements after do to be executed at least once, and use the other one to force the check before executing the statements.
I don't get the point of the poll it's same as the '\n' VS. endl
I think that you should use what you need depending on the case.
Why not start a poll: "which keyword is best, 'class' or 'int'?"
lol, like your post Sang-drax, actually that's true maybe you'll find one soon!:D
Do-while is never necessary; you can just copy the contents of the inside of the loop to before the loop :)
do while vs while is not something you vote on. each loop does slightly different things and you use whatever is proper for your situation. sheeesh.
for things such as a game loop i perfer while, example:
Code:
cout<<"Enter a letter";
cin>>letter
while (letter != 'Q' || 'q')
{
.......
.......
.......
}
I always use
While loops because it is easier to read through the code for me.. and it solves just about every while loop problem i come across.. however i will use a
do. while when i am inputing from a file to use as a priming read.. saves me from having to repeat the same code
Code:do
{
infile >> bob;
}
while(!infile.eof());
:) Correct me if I'm wrong, but if you're reading from a file, shouldn't you check for eof right away (i.e. instead of after the loop)? I mean, if the file's empty, the first iteration of the do-while will have been a waste of cpu time, wouldn't it?
is it? honestly i cannot remember.. so long ago that i actually had to read from files.. for some reason i was thinkin' that u had to do a priming read before you could check the eof() status.. guess i was wrong :P then i guess i never use a do..while loop..
:D
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! :D ) 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
:) Tegwin, as you may remember, I said "Correct me if I'm wrong" :D 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;
}
Your call. I've made mine. ;)Quote:
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
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? :p
No, it's because people just tend to use it. The warning seems fairly clear, but I get your meaning. ;)
-Skipper
That's good to know, but the problem is, I don't get yours :D What's wrong with people tending to use it?
Your call, Chief. I posted a direct quote from The Man's book. If you know better, go for it!
-Skipper
That's the problem :( I don't know better, but I don't get it either :DQuote:
If you know better, go for it!
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
I see. Thanks for the explanation, I never thought of it that way :)
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.
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
I could be wrong, but I think do-while and while were part of the original C language, and basically C++ just inherited them.
I'm with some of the other guys.
Each is useful in different contexts. If you don't need to go into the loop at least once, why bother.
DS
Because sometimes you only want to go into the loop if a certain condition is true; thus, with a while loop, you can cut out an extra if statement which might make your code uglier.
The poll is now closed.