C Language



Admission Enquiry Form

  

While Loop in C

What is While Loop in C ?

While loop is also called unknown loop.It is pre-tested loop or entry control loop,in which condition is checked first.If condition is true then control will enter the while loop block and execute the block ,otherwise it will exit the while loop block. The syntax of while loop is given below.

Syntax of While loop

initialization;
while(condition)
{
//statements.
increment/decrement;
} //end of while loop



Example of While loop:

//example of while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int num=1;
while(num<=5)
{
printf("%d ",num);
num++;
}
}

Output:

1 2 3 4 5

In the above example,initially value of num=1. Then, control will come to while loop and check the condition.It will be true for 5 times ,firstly value of num will be less than 5.When it will be incremented to 5 ,then it will be equal to 5.But, after 5 when num value will be incremented to 6 then condition will become false and control will exit the while loop block.