difference between do and while loops

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

difference between do and while loops

 
0
  #1
May 11th, 2009
What's the main difference between while(){ } and do{ }while() loops? From looking at it, it looks like the former evaluates before it performs the iteration and the latter does the evaluation after each iteration... is that correct? Why would I pick one over the other? Is it mainly just a readability thing? Like
  1. bool fail = false;
  2. do{
  3. //something
  4. } while (fail = true);
rather than starting fail as true?
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 32
Reputation: VatooVatoo is an unknown quantity at this point 
Solved Threads: 2
VatooVatoo's Avatar
VatooVatoo VatooVatoo is offline Offline
Light Poster

Re: difference between do and while loops

 
0
  #2
May 11th, 2009
if you want do run a loop at least once, it is better to use do-while loop.
lets explain more. look this sample code:

  1.  
  2. FILE *fp;
  3. do
  4. {
  5. read file and assign some values
  6. }
  7. while( some data in file is equal to special value so file read again );
  8.  
  9. will use last data from file

in this example if you dont use do-while loop structure you need file once before loop and also read in loop

as you say "it is mainly just a readability thing"
Last edited by VatooVatoo; May 11th, 2009 at 4:19 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 32
Reputation: VatooVatoo is an unknown quantity at this point 
Solved Threads: 2
VatooVatoo's Avatar
VatooVatoo VatooVatoo is offline Offline
Light Poster

Re: difference between do and while loops

 
0
  #3
May 11th, 2009
if you want do run a loop at least once, it is better to use do-while loop.
lets explain more. look this sample code:

  1.  
  2. FILE *fp;
  3. do
  4. {
  5. read file and assign some values
  6. }
  7. while( some data in file is equal to special value so file read again );
  8.  
  9. will use last data from file

in this example if you dont use do-while loop structure you need read file once before loop and also read in loop

as you say "it is mainly just a readability thing"
Last edited by VatooVatoo; May 11th, 2009 at 4:20 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: difference between do and while loops

 
0
  #4
May 11th, 2009
awesome, thanks
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC