What is do while loop?

If you don't know number of iteration in advance. How much iteration go long.? The do while loop is same as a while loop with one important difference.

The body of do while loop is executed once, before checking the condition. Hence, the do while loop is executed at least once.

How it work?

It execute until the condition not go false. Keep preforming iteration until condition not become false.

Syntax of do while loop statement:

	
    do
    {
    statement;
    }
    while(condition);
    

Example of Do While Loop program:

Let's suppose you want to print the number from 1 to 10. You can't write the 10 times printf function to print the all number. It is very time consuming process. To solve this problem we use while loop condition.

Fibonacci Program:

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

void main()
{
	int i=1, n;
	int f1 = 0, f2 = 1, f3;
	clrscr();
	printf("Enter the Number :- ");
	scanf("%d",&n);
	printf("%d %d ",f1,f2);
	do
	{
	f3=f1+f2;
	printf("%d ",f3);
	f1=f2;
	f2=f3;
	i++;
	}
	while(i<=n-1);

	getch();
}

Output:

	
    Enter the Number :- 5
    0 1 1 2 3 5
    

Previous while loop condition Next for loop condition

Advertisement here

Share this Page.

Post Your Comment