Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-02-2009
Member
 
Join Date: Feb 2009
Posts: 4
What is the difference between while statement & do statement?

Hi,

What is difference between while statement & do statement?
Reply With Quote
  #2  
Old 25-02-2009
Member
 
Join Date: Jan 2009
Posts: 44
Re: What is the difference between while statement & do statement?

Do while .. first does some code and then checks for a condition.
do-while is a exit controlled loop.
and
While first validates the condition before processing any code in the loop.
while is an entry controlled loop.
This is the major difference between the two!
Reply With Quote
  #3  
Old 25-02-2009
Member
 
Join Date: May 2008
Posts: 26
Re: What is the difference between while statement & do statement?

A while statement (pre test) checks at the beginning of a loop to see whether the next loop iteration should occur. A do while statement (post test) checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the loop body at least once.
Reply With Quote
  #4  
Old 25-02-2009
Member
 
Join Date: May 2008
Posts: 44
Re: What is the difference between while statement & do statement?

A "do while....." statement is a looping instruction to a program to repeat a stage in the program while some condition is true - e.g while a variable is negative, or, while one variable is less than another.

A "do for ....." statement is a looping instruction to a program to repeat a stage in the program a set number of times - e.g for steps = 1 to 10, or, for steps = 1 to (some variable).
Reply With Quote
  #5  
Old 25-02-2009
Member
 
Join Date: Jan 2009
Posts: 38
Re: What is the difference between while statement & do statement?

Well, there is basically only one difference.

In do..while

int count = 0;
do
{
cout<<"Hello World";
}
while(count = -5);

the body is executed first and only then does the compiler come across the while( ) statement and the condition is checked. If true, it goes back to the do line and executes the body of the loop until the condition becomes false.

The output of the the above construct would be

Hello World

In the while statement, the condition is checked first, and only if it returns a true value is the body executed

int count = 0;
while(count = -5);
{
cout<<"Hello World";
}

There is no output for the above construct.

Hope the difference is clear.

If you need more assistance, I suggest you read Balaguruswamy's 'Let us C'
Source(s):
"Let us C"
by Balaguruswamy
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is the difference between while statement & do statement?"
Thread Thread Starter Forum Replies Last Post
How to use Else and Else If Statement in PHP? Orton Software Development 5 05-03-2010 06:49 AM
How to use If statement in PHP? Deabelos Software Development 4 09-02-2010 06:14 AM
REDIM statement Amaresh Software Development 3 28-11-2009 09:27 AM
What is Echo? What is the difference between Echo and Print statement in PHP? Preetish Software Development 3 05-09-2009 01:41 PM
What is the difference between Msgbox Statement and MsgboxQ function? SamsherR Software Development 2 28-02-2009 10:32 PM


All times are GMT +5.5. The time now is 12:57 AM.