944,007 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2949
  • C RSS
May 11th, 2009
0

difference between do and while loops

Expand Post »
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?
Similar Threads
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
May 11th, 2009
0

Re: difference between do and while loops

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.
Reputation Points: 20
Solved Threads: 2
Light Poster
VatooVatoo is offline Offline
39 posts
since Jan 2007
May 11th, 2009
0

Re: difference between do and while loops

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.
Reputation Points: 20
Solved Threads: 2
Light Poster
VatooVatoo is offline Offline
39 posts
since Jan 2007
May 11th, 2009
0

Re: difference between do and while loops

awesome, thanks
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: create output files
Next Thread in C Forum Timeline: Using float to define boolean type in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC