Results 1 to 3 of 3

Thread: Difference between do-while and while loops

  1. #1

    Thumbs up Difference between do-while and while loops

    while statement:
    Code:
    while (conditional expression)
    {
    ...block of statements to execute...
    }
    The while loop continues to loop until the conditional expression becomes false.
    Once this expression become false, the control is transferred out of the loop. On exit, the program continues with the statement immediately after the body of the
    loop. For example:
    Code:
    i=0;
    while(i<10)
    {
    printf(“Hello world\n”);
    i++;
    }
    This statement will print “Hello world” 10 times in a new line and come out of the loop when ‘i’ become 10.
    Do statement:

    Code:
    do
    { ...block of statements to execute...
    }while(conditional expression);
    While construct checks the conditional expression before the loop is executed.
    Sometimes it is necessary to execute the body of the loop before the conditional expression is evaluated. Such situations are handled by do-while loop construct. On reaching the do statement, the body of the loop is evaluated and at the end of the loop, the conditional expression is checked for true or false. If true,it continues to evaluate the body again and when condition become false, the control is transferred to the statement immediately after the while statement.
    Code:
    do
    {
    printf( “Input a character\n”);
    ch=getch( );
    }
    while(ch=’n’);
    This segment of program reads a character from the keyboard until ‘n’ is keyed
    in.
    techforum4u.com Member!!

  2. #2
    Senior Member semaphore.v's Avatar
    Join Date
    Jun 2013
    Location
    India
    Posts
    100
    Rep Power
    2

    Re: Difference between do-while and while loops

    In While loop the condition is tested first and then the statements are executed if the condition turns out to be true.
    In do while the statements are executed for the first time and then the conditions are tested, if the condition turns out to be true then the statements are executed again.
    techforum4u.com Member!!

  3. #3
    Junior Member mk1234's Avatar
    Join Date
    Nov 2013
    Location
    new delhi
    Posts
    3
    Rep Power
    0

    Re: Difference between do-while and while loops

    while loop check the conditions before execute the contents but do while loop execute the contents before checking the conditions.
    techforum4u.com Member!!

Similar Threads

  1. JavaScript Loops
    By christin1a in forum JavaScript
    Replies: 0
    Last Post: 09-27-2011, 05:40 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
  •  



Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.