| | |
difference between do and while loops
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
rather than starting fail as true?
c Syntax (Toggle Plain Text)
bool fail = false; do{ //something } while (fail = 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
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
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:
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"
lets explain more. look this sample code:
C Syntax (Toggle Plain Text)
FILE *fp; do { read file and assign some values } while( some data in file is equal to special value so file read again ); 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.
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:
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"
lets explain more. look this sample code:
C Syntax (Toggle Plain Text)
FILE *fp; do { read file and assign some values } while( some data in file is equal to special value so file read again ); 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.
![]() |
Similar Threads
- Changing For loops to while loops and vice versa (Python)
- Difference among loops (C)
- O-notation (C++)
Other Threads in the C Forum
- Previous Thread: create output files
- Next Thread: Using float to define boolean type in C
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi





