do-while Statement in C Programming Language

Let us understand do while statement in C with help of an example.

syntax:

Explanation:
Here, while and do are the keywords in C language which is know to the compiler. Condition can be any expression. This is very similar to while loop.

Here, the block of statement enclosed in the opening and closing braces after the keyword do is executed at least once. After the execution of the block of statement for the first time, the condition in the while is executed.

If the condition is satisfied then the block of statement inside the do block is executed. After the execution of block of statement, again condition is check. If the conditional expression returns true, the block of statement is executed again. This process is followed till the conditional expression returns false value.

Note:
It is necessary to write semicolon (;) after the while(condition) as shown in the syntax else you will get an error.

The example for while loop (while Loop in C) can re-written as follows:

Output:

You may also like...

23 Responses

  1. Anonymous says:

    Nice explanation. This site is like a book. something very different and interesting

  2. Anonymous says:

    great

  3. sushma says:

    a wonderful site to learn c

  4. Anonymous says:

    this site is 21st century learn c site

  5. moni_moni says:

    clear and short cut and easiest way to learn c

  6. Anonymous says:

    nice

  7. Anonymous says:

    LOVE THIS

  8. Manoj Rathod says:

    AwesoMe Site!!! I loved this site It solved lot of doubts which were in my mind Thank You learnconline.com 🙂

  9. Anonymous says:

    good work…

  10. Mushtaq Ali says:

    while(i!=5)
    here even 1 2 3 4 are not equal to 5 then hw can we get the output this is repeated 5 time for the first four steps

  11. Mushtaq Ali says:

    do{
    i=i+1;
    printf(“%d This will be repeated 5 times\n”, i);
    }while(i!=5);
    here 1 2 3 4 are also nt equal to 5 na (i!=5), then the o/p shud b end of the program for the first 4 steps 1 2 3 4…

  12. Anonymous says:

    Before checking while condition it will executed so it will executed for 5 also,
    like while(4!=5)it will again enter the block & will execute once it executed only it will check for next condition while(5!=5)

  13. Unknown says:

    very good site for the biginners

  14. Anonymous says:

    wht is clrscr

  15. LearnCOnline says:

    clrscr is an inbuilt function used to clear the output window screen

  16. Anonymous says:

    reply to mushtaq ali: here we condition while(i!=5).that means we are asking to execute only if i is not equal to 5.so 1 ,2 3 and 4 are not equal to 5.so program executes and gives output.bt after 4 comes 5 ..which is equal to 5.then our condition while(i!=5) becomes false and comes out of program and stops execution.

  17. awesome site!!!!!
    Is there any site like this for learn java & .net?

  18. Anonymous says:

    Interesting to learn

  19. Anonymous says:

    Thanks

  20. Anonymous says:

    whats difference between while and do while?

  21. This is very good site for every one who want to learn c , i’m also a learner of urs… thanks a lot ….. i have a doubt…..
    Where to use while statement and and where to use do while statement ? In which way they differ practically

  22. LearnCOnline says:

    In the while loop, the condition is first evaluated and then the block of code is executed based on the condition.

    In do-while loop, first the block of statements inside do is executed and then while condition is evaluated.

    Even if the condition is not satisfied for the first time, the block of statements inside do is executed at least once.

    Hence, use do-while loop if you want your block of statements to be executed at least once.

    Thanks,
    LearnCOnline Team

  23. Freshkidd"s says:

    #include
    #include

    int main(){
    int i=0;
    do{

    i=i+1;
    printf(“%d LISTEN TO YOUR COMPUTER DIE LMAO!!\n”,i);
    }while(i!=10);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *