Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Since both are control flows, why have two(2) statements that have the same purpose? How do they differ?

do
{
//code here..
} while (bool == false);

while (bool == false);
{
//code here..
}


I mean sh*t, the only difference I see, is that one doesn't have a 'do' while the other one does. Sigh.
Posted

It's quite simple. They are both loops that execute while a condition is true. One checks the condition before the loop, the other checks it after the loop. This leads to one being 0 or more times, and the other 1 or more times. That's all. So you pick one of them based on that requirement.
 
Share this answer
 
Comments
Espen Harlinn 8-Apr-11 17:54pm    
Nice and simple reply :)
Nish Nishant 8-Apr-11 18:10pm    
Thanks Espen!
Sergey Alexandrovich Kryukov 8-Apr-11 19:52pm    
Sure, a 5.
--SA
Nish Nishant 9-Apr-11 8:35am    
Thank you!
Albin Abel 9-Apr-11 0:40am    
Clear answer. My 5
For different logics. Condition is not checked in the very beginning in first cases, but is checked in the second case. Your example will not compile (bool is the reserved word, must be a variable). Now, consider it is a variable, say Condition. Then (Condition == false) make no sense, use (!Condition). You're trying to make Boolean from a Boolean, makes no sense.

Now, you cycles are infinite. Why? In real life the condition should be re-calculated in each cycle. When you do that, you will see the difference. For example, with the fist construct you cannot make a loop running 0 times — first iteration always run, with the second one you can, as condition may be true or false, depending in its calculation.

Now, posting question like that makes little or no sense. Read the manual first, with sample. Asking such question is very ineffective and waste your time, as well as others'.

—SA
 
Share this answer
 
Comments
Herboren 8-Apr-11 16:40pm    
That was just an example cause im at work I cant whip something up fast enough to prime my question, but I understood what Naerling posted. The question seemed perfect to everyone else, honestly 2 people answered what i was looking for. =) Maybe it was the lack of punctuation that threw you off.
Sander Rossel 8-Apr-11 16:41pm    
I think you kind of missed the point of the question... He was simply asking for the difference between the two, the syntax is just an example.
Edit: Oops, Herboren beat me to it ;)
Sergey Alexandrovich Kryukov 8-Apr-11 19:57pm    
Are you serious? missed the point? How about your logic? Perhaps you think a question like "what's the difference between {0} and {1}" is valid? Thank think again.
And I guess you voted accordingly?
(Just a note: I'm not going to vote; and frankly, this question does not deserve any answer at all; jut RTFM would be quite enough).
--SA
Sander Rossel 9-Apr-11 5:11am    
Well, if you put it like that it might have been something he could have looked up himself. But I for one, did not even know there was a Do While. By Herboren posting this question others may also learn from it. Besides, he asked a clear question, using <pre> tags and in understandable English. That alone deserves a good answer (even just a link where it is explained). Maybe you have heard of "There are no dumb questions"? And note that "gimme codez plz" is not a question :)
So while your answer is a good and detailed one I do not like the attitude that comes with it.
Herboren 8-Apr-11 16:43pm    
Lol moar fail, its okay Naerling, you answered my question and that all that matters.
I just found this...
A while loop might never execute a statement if the expression is false but a do while will always execute the statement at least once.

Sounds logic, since your first example does something and checks an expression after that while your first example first checks if it needs to do something.

Here is the link I found: http://cplus.about.com/od/learningc/ss/clessonfive_4.htm[^]
 
Share this answer
 
Comments
Herboren 8-Apr-11 16:27pm    
Ok so to use an loose anology:

while()
I am the while at the beginning and I am the lock, you can not access my statement unless your expression is true!

do while()
I will let you process my statements once or possibly more than once, but if I find your expression to be true then you are no longer allowed to process what I have to offer!

I know it sound ghey but, please tell me I hit the nail on the head?
Espen Harlinn 8-Apr-11 17:48pm    
>> if I find your expression to be true then
>> you are no longer allowed to process what
>> I have to offer!

You got it exactly wrong - the loop will execute until the expression evaluates to false, I think SAKryukov and Nishant made a teriffic effort to head you in the right direction.

Sander Rossel 8-Apr-11 17:56pm    
Oh, only noticed that now :)
Should be true, not false.
Espen Harlinn 9-Apr-11 3:08am    
OK :)
Sander Rossel 8-Apr-11 16:35pm    
Yeah, that's it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900