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 26-11-2009
Member
 
Join Date: Nov 2009
Posts: 580
Differentiate between Do-While loop and While loop

Hi Guys,

I am learning the C language. I was very easy to understand the concept of While loop. But when portion of C language came to Do-While language, It was too confusing. I am not able to understand what basic difference in between Do-While loop and While loop. How can we determine which loop will be suitable for the particular test condition?

What the difference between Do-While loop and While loop? Can you able to explain it with example?
Reply With Quote
  #2  
Old 26-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: Differentiate between Do-While loop and While loop

Hi,

Following are the basic difference between Do-While loop and While loop:

I) The code for "Do" from "Do-While" is executes at-least once.But in the "While" loop if test condition fails at the first time then the program code won't be executed.

II) "Do-while" loop tests loop condition first then start proceeding. While in "Do-While" tests loop condition after the execution of the body of the "While loop".
Reply With Quote
  #3  
Old 26-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Differentiate between Do-While loop and While loop

Hi Friend,

I don't know what is exact difference between Do-While loop and While loop. But if you carefully study the below code for Do-While loop and While loop, you may get some idea to solve you problem.

Do-While loop:
Quote:
class demo {
public static void main(String[] args){
int count_demo = 1;
do {
System.out.println("Count is: " + count_demo);
count++;
} while (count_demo <= 15);
}
}

While loop:
Quote:
class demo {
public static void main(String[] args){
int count_demo = 1;
while (count_demo < 15) {
System.out.println("Count is: " + count_demo);
count_demo++;
}
}
}
Reply With Quote
  #4  
Old 26-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Differentiate between Do-While loop and While loop

Hi,

Do-While loop and While loop are quit confusing for me also. We see that the the while loop terminates as soon as condition mismatch, and therefore the code for whiles loop doesn't run anymore.
But in Do-while loop it is reverse, even though condition fails the code for do block runs for single time.

I hope this information may helpful for you..
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Differentiate between Do-While loop and While loop"
Thread Thread Starter Forum Replies Last Post
Watercooling: Single loop or Dual Loop Akolekar Hardware Peripherals 3 21-10-2011 10:52 PM
Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work mADiRAkSHii Operating Systems 4 25-01-2011 07:23 PM
How to use For-Each Loop in PHP? Bigga Lexx Software Development 5 06-03-2010 01:16 AM
How to use Do-While Loop in PHP? Zavier Software Development 5 06-03-2010 12:34 AM
Which is best for iterator: For loop or while loop Leeland Software Development 4 18-01-2010 06:03 PM


All times are GMT +5.5. The time now is 11:11 PM.