Example for while loop

Example for while loop:

/* Wap to print  natural numbers from 1 to n*/
#include< stdio.h >
main()
{
int n,i=1;

printf("\nEnter the value for n ");
scanf("%d",&n);

while(i<=n)
{
    printf("\n%d",i);
    i++;
}

}
In the above program looping variable i is initialised to 1 and the value for n will be accepted. Then i<=n looping condition will be evaluated if it is true then control of execution  enters into the body of loop and prints the value of variable i and value of i will be incremented by 1 and the looping condition will be evaluated once again if it evaluates to true then once again the body of loop will be executed. This process will be repeated until the looping condition is true.

Comments