![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
do while = [actíon]+ while = [action]*
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#7 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 546
Rep Power: 4
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|