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

bool fail = false;
do{
//something
} while (fail = true);

rather than starting fail as true?

Recommended Answers

All 3 Replies

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:

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"

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:

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"

awesome, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.