Results 1 to 4 of 4

Thread: Differentiate between Do-While loop and While loop

  1. #1
    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?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    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".

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    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:
    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:
    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++;
    }
    }
    }

  4. #4
    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..

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,572,325.65068 seconds with 16 queries