Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 24th, 2005, 11:45 AM   #1
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
while/do while?

Could someone tell me what is the practical diference between while and do while? I know that in while the condition is evaluated before the statment as in the do while it is evaluated at the end.

But in my (very) limited programming experience I don't know where these diferente cases apply...

Thanks...
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jul 24th, 2005, 11:54 AM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
int x = 5;
while(x-- > 1)
{
  cout<<x;
}  //prints 4,3,2,1

cout<<endl;
x = 5;

do
{
  cout<<x;
}while(x-->1);   //prints 5,4,3,2,1
OpenLoop is offline   Reply With Quote
Old Jul 24th, 2005, 12:00 PM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by Broax
Could someone tell me what is the practical diference between while and do while?
do while always performs the action at least once. while checks the condition first, so it may not perform the action at all.

do while = [actíon]+
while = [action]*
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Jul 24th, 2005, 12:13 PM   #4
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
Yes... I get it now, but is there any situatuin where while can be used and do while can't? (or the other way around?)
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jul 24th, 2005, 12:21 PM   #5
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by Broax
Yes... I get it now, but is there any situatuin where while can be used and do while can't? (or the other way around?)
there is really nothing more to it. You can manipulate the conditions as you please, and substitute one for the other.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Jul 24th, 2005, 12:34 PM   #6
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
hmmm... I see!

Thanks for the help, then! I'll get back to german studying and later back to c++!
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jul 24th, 2005, 1:14 PM   #7
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 546
Rep Power: 4 Ancient Dragon is on a distinguished road
well, one example when I would use do while instead of while is for interating through the file system using _findfirstfile() and _findnextfile(), or equivalent os-specific functions. The _findfirstfile() locates the first file, so the end of the do-while would be the _findnextfile().
_findfirstfile()
do {
  // blabla
} while( _findnextfile() );

coding the above with while loop would be more problematic.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC