while loop

The while loop is used when you want to execute a single line statement or block of statements repeatedly with checkd condition before making an iteration.

The syntax is:-

while ( condition )

statement 1;
statement 2;
statement 3;
statement n;


Suppose we want to print 1 to 10 numbres on display, what to do?
One way is to write 10 number of printf() function in C program but if  numbers are too big like 1 to 1000, C provides one another way to print by using single printf()
that is called loops,  See example below:-

int main()

int count = 0;

while (count <= 10)

printf(“%d \t”,count);



When you compile and execute above C program, this will print 0 to 10 numbers. here ‘\t’ is used to place a tab space between two numbres,

Explanation :-
The initial value of count is 0, very first time the while loop condition is true because 0 is less than 10, in next line printf() function prints current value of the count, immediately after printf() function we are incrementing value of count by one by applying post increment operator on count, again compiler goes to  check  while loop condition likewise  the while loop is successfully execute until the value of count becomes 11, this time while loop condition is false and compiler comes out from the while loop, the output will be:-
$  ./a.out
0    1    2    3    4    5    6    7    8    9    10


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.