468,463 Members | 2,046 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

Difference between while and do while

Hi. What's the difference between while and do while?
Dec 24 '07 #1
9 18782
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'));
"Logan Lee" <lo*********@student.uts.edu.auwrote in message
news:47***********************@news.optusnet.com.a u...
Hi. What's the difference between while and do while?

Dec 24 '07 #2
Logan Lee wrote:
Hi. What's the difference between while and do while?
Where do you see the test?

--
Ian Collins.
Dec 24 '07 #3
Logan Lee <lo*********@student.uts.edu.auwrites:
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 24 '07 #4
Logan Lee wrote:
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."

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 24 '07 #5
"Logan Lee" writes:
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.
Dec 24 '07 #6
In article <5t*************@mid.individual.net>,
osmium <r1********@comcast.netwrote:
>"Logan Lee" writes:
>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.
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.

Dec 24 '07 #7
Kenny McCormack wrote:
In article <5t*************@mid.individual.net>,
osmium <r1********@comcast.netwrote:
>>"Logan Lee" writes:
>>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.

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.

Dec 25 '07 #8
ga*****@xmission.xmission.com (Kenny McCormack) writes:
{};
while () do {};
The semicolons and the "do" keyword should be dropped (and some
content should be added to the parentheses and braces).
--
Peter Seebach on C99:
"[F]or the most part, features were added, not removed. This sounds
great until you try to carry a full-sized printout of the standard
around for a day."
Dec 25 '07 #9
Logan Lee wrote:
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?
Dec 25 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

52 posts views Thread by Rick | last post: by
7 posts views Thread by Mahesh Kumar Reddy.R | last post: by
147 posts views Thread by Michael B Allen | last post: by
12 posts views Thread by Howard | last post: by
23 posts views Thread by Naha | last post: by
11 posts views Thread by Rene | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.