While loop in C

The While loop


This is an entry controlled looping statement. It is used to repeat a block of statements until condition becomes true.
In this, first of all condition is checked and if it is true then group of statements or body of
loop will be executed. It will execute again and again till condition becomes false.

Syntax :

while(condition)
{
statements;
increment/decrement;
}

In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the loop and executes the block of statements associated with it. At the end of loop increment or decrement is done to change in variable value. This process continues until test condition satisfies.

The operation of the while loop is illustrated in the following figure.





















Simple Example :

#include <stdio.h>
#include <conio.h>

void main()
{
int x;
clrscr();
x=1;
while(x<=5)
{
printf("\nHi");
x = x + 1;
}
getch();
}

Output :









Description :

Here the value of x is 1. Now the compiler will check the while condition, since 1 is less than 5, hence the control goes into the while loop and will print 'Hi', then the next statement x=x+1 will execute which change the value of x=2 and the control again goes to check the condition of while loop which is again comes out to be true as 2 is less than 5 and hence it will again print 'Hi'. Similarly this process continous until the value of x is greater than 5 that is when the value of x becomes 6 then the condition will false and hence the control will be out of the while loop.
Hence 5 times 'Hi' is printed and in the print statement we has used the '\n' escape sequence which means next line hence the every 'Hi' is printed in new line.

Note :

We will discuss more programs in our C programs section.


     Prev                                                   NEXT

C loop control structure

C loop control structure


What is a loop


A loop is a part of code of a program which is executed repeatedly.

The versatility of the computer lies in its ability to perform a set of instructions repeatedly.
This involves repeating some portion of the program either a specified number of times or until
a particular condition is being satisfied. This repetitive operation is done through a loop control structure.

A loop is used using condition. The repetition is done until condition becomes condition true.

A loop declaration and execution can be done in following ways.

  • Check condition to start a loop
  • Initialize loop with declaring a variable.
  • Executing statements inside loop.
  • Increment or decrement of value of a variable.
Types of Looping statements:

There are two types of looping statements:
  1. Entry control loop
  2. Exit control loop



1. Entry control loop :

In this type of loop, the test condition is checked first before the loop is executed.

Examples of this looping statements are :
(click on the red links...)

2. Exit control loop :

In such type of loop, the loop is executed first. Then condition is checked after block of statements are executed. The loop executed atleat one time compulsarily.

Example of this looping statement is :



     Prev                                                   NEXT