Do while Loop in C Programming language

  • In C programming languages, A do while loop is a control flow statement that executes a block of code at least once
  • The do…while loop in C programming checks its condition at the bottom of the loop
  • A Do-while runs at least once even if the condition is false because the condition is evaluated, after the execution of the body of loop.

Syntax:-

Do
{  
//code to be executed  
}
While (condition);  

Flow chart of do while

\"Do
Do while in C programming language flow chart

For Example

#include<stdio.h>
#include<conio.h>
int main()
{
	int x=0;
	do
	{
		printf(\"Value of variable x is: %d\\n\", x);
		x++;
	}while (x<=3);
	return 0;
}

Output

Value of variable x is: 0
Value of variable x is: 1
Value of variable x is: 2
Value of variable x is: 3

A Simple Program to illustrator difference between while loop and do while loop

#include<stdio.h>
#include<conio.h>

int main()
{
    int x=0;
    while(x==1)
    {
	printf(\"while loop Example \");
    }
    printf(\"Out of loop\");


return 0;
}

#include<stdio.h>
#include<conio.h>

int main()
{
   int x=0;
   do
   {
	printf(\"do-while loop example\\n\");
   }

while(x==1);
   printf(\"Out of loop\");
return 0;
}