Results 1 to 5 of 5

Thread: What is the difference between while statement & do statement?

  1. #1
    Join Date
    Feb 2009
    Posts
    4

    What is the difference between while statement & do statement?

    Hi,

    What is difference between while statement & do statement?

  2. #2
    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!

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

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

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

Similar Threads

  1. How to use Else and Else If Statement in PHP?
    By Orton in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 06:19 AM
  2. How to use If statement in PHP?
    By Deabelos in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 05:44 AM
  3. REDIM statement
    By Amaresh in forum Software Development
    Replies: 3
    Last Post: 28-11-2009, 08:57 AM
  4. Replies: 3
    Last Post: 05-09-2009, 01:11 PM
  5. Replies: 2
    Last Post: 28-02-2009, 10:02 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,638,146,193.77169 seconds with 17 queries