Difference between while and do-while in C - C-Tutorial

Latest

Saturday, 29 October 2016

Difference between while and do-while in C

Difference between while and do-while in C


WHILE
DO-WHILE
while ( condition)
 {
statements; //body of loop
}
Do
{
statements; // body of loop.
} while( Condition );
In 'while' loop the controlling condition appears at the start of the loop.
In 'do-while' loop the controlling condition appears at the end of the loop.
The iterations do not occur if, the condition at the first iteration, appears false.
The iteration occurs at least once even if the condition is false at the first iteration.
#include<stdio.h>
#include<conio.h>

main()
{
int i=1,n;
clrscr();
printf("\nEnter how many numbers:");
scanf("%d",&n);
printf("\nNatural Numbers Are:");
while(i<=n)
            {
                        printf(" %3d",i);
                        i++;
            }
}

main()
{
int i=1,n;
clrscr();
printf("\nEnter how many numbers:");
scanf("%d",&n);
printf("\nNATURAL NUMBERS ARE:");
            do
            {
                        printf("%5d",i);
                        i=i+1;
            }
            while(i<=n);
}

No comments:

Post a Comment