do while loop in C programming language

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

syntax:

do
{
	/*block of statement*/
}while(condition);

Explanation of do while loop in C:
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 in C.

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 in C can re-written as follows:

#include<stdio.h>
#include<conio.h>
void main()
{
    int i=0;
    clrscr();
    
    do{
        i=i+1;
        printf("%d This will be repeated 5 times\n", i);
    }while(i!=5);
    
    printf("End of the program");
    getch();
}

Output of the above do while loop example:

1 This will be repeated 5 times
2 This will be repeated 5 times
3 This will be repeated 5 times
4 This will be repeated 5 times
5 This will be repeated 5 times
End of the program

You may also like...

24 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);
    }

  1. July 4, 2020

    […] continue statement works similar to break statement in C. Instead of terminating the loop statement, it will force next iteration of the loop. continue statement can be used with for loop, while loop and do-while loop. […]

Leave a Reply

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

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet