Ads block

Banner 728x90px

While Loop Statement

A while loop is a pre-tested loop that means controller checks the condition first and then enters in the body of while loop to execute the statements of the body. The while loop is used to execute the same block of code again and again, and repetition occurs when the block of code is finished. The execution of same block will continue until the condition is evaluated to false.

Here, the format of the while-loop is given:

while(condition)
 {
   //body of while loop
 }

Flowchart of while loop

A while loop must have an update statement such as increment/decrement. So that, the controller can check the same condition for the execution of next loop after the end of current loop.

Here is a suitable example of while loop that's will help you to understand it.

//Program to print statements using while loop.
#include<stdio.h>
int main()
{
  int i=1;

  while(i<=5)
   {
    printf("loop: %d\n",i);
    i++;   //increment statement
   }

  return 0;
}

loop: 1
loop: 2
loop: 3
loop: 4
loop: 5



Let's see the another example of while loop.

Program to print table.

#include<stdio.h>
void main()
{
  int i=1,n;
  printf("Enter a positive integer: ");
  scanf("%d", &n);

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

Enter a positive integer: 10

10x1 = 10
10x2 = 20
10x3 = 30
10x4 = 40
10x5 = 50
10x6 = 60
10x7 = 70
10x8 = 80
10x9 = 90
10x10 = 100