Do While Loop - C Tutorial

Do While Loop

The do while loop is similar to the while loop with one important difference. The body of do...while loop is executed once, before checking the test expression. Hence, the do while loop is executed at least once.

do while is an exit controlled looping statement.

Syntax of Do While loop

do
{
     //Code to execute
}
while (testExpression);

Do While Loop - Example

    
#include <stdio.h>
int main()
{
	int num;
	char choice;
	
	do
	{
		printf("Enter the number :");
		scanf("%d",&num);
		printf("Square of %d is %d\n",num,num*num);
		flushall();
		printf("Do you want to Enter another number (y/n)?");
		scanf("%c",&choice);
	}while(choice=='y');
	
	return 0;
	
}