C

while loop

while loop can be addressed as an entry control loop. It is completed in 3 steps.

  • Variable initialization.( e.g int x=0; )
  • condition( e.g while( x<=10) )
  • Variable increment or decrement ( x++ or x-- or x=x+2 )

Syntax:

variable initialization;
while (condition)
{
    statements;
    variable increment or decrement; 
}

Example:

//Write a Program to Print First 10 Natural Numbers using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
    int i;
    i = 1;
    while(i <= 10)
    {
        printf("%d ",i);
        i++;
    }
    getch();
}

Output:

Command Prompt
1 2 3 4 5 6 7 8 9 10



Subscribe us on Youtube

Share This Page on


Ask Question