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?
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".
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++;
}
}
}
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..