Difference between while and do while


Ad

Advertisements

D

Dinesh P

In While loop the condition is tested first and then the statements are
executed if the condition turns out to be true.
In do while the statements are executed for the first time and then the
conditions are tested, if the condition turns out to be true then the
statements are executed again.

A typical scenario to use do While loop.
I would like to get a specified input from user. Here first I will get the
input then I will check whether we got the specified input other wise we
will again ask for the input.
eg.,
do
{
char input;
printf("say yes or no :(y/n)";
input = getchar();
}while(!(input == 'y' || input == 'n'));
 
K

Keith Thompson

Logan Lee said:
Hi. What's the difference between while and do while?

What does your textbook say?

I'm not trying to be unhelpful. This is very elementary question,
something that any decent textbook or tutorial should answer.
 
E

Eric Sosman

Logan said:
Hi. What's the difference between while and do while?

Are you the same "Logan Lee" who's lecturing us all on
how to do static code analysis? If you're going to analyze
code, wouldn't it be a good idea to learn the language first?

Get a C textbook. Usenet is a fine way to transmit some
kinds of knowledge, but it's not well suited to delivery of
"the 101 course."
 
O

osmium

Logan Lee said:
Hi. What's the difference between while and do while?

A do while is used for a block of code that must be executed at least once.
These situations tend to be relatively rare, thus the simple while is more
commonly used.
 
Ad

Advertisements

K

Kenny McCormack

A do while is used for a block of code that must be executed at least once.
These situations tend to be relatively rare, thus the simple while is more
commonly used.

The best way to express this is that:

do {<statement>} while (<expr>);

is equivalent to:

{};
while () do {};

i.e., do it once, then enter into a normal "while" loop.
 
S

santosh

Kenny said:
The best way to express this is that:

do {<statement>} while (<expr>);

is equivalent to:

{};
while () do {};

i.e., do it once, then enter into a normal "while" loop.

Yes. In C syntax that would be:

{ };
while () { }

You could also place the loop at the bottom of the previous block.
 
Ad

Advertisements

J

James Kuyper

Logan said:
Hi. What's the difference between while and do while?

You've repeatedly asked this newsgroup very elementary questions about
C. Would you please bother reading an elementary C textbook to find out
the answers to these questions? If you've already done so, and you still
have this many questions about matters this elementary, may I politely
suggest that computer programming doesn't seem like a good career choice
for you?
 

Top