Loops Used in C | For Loop, While Loop and Do-While Loop

Loops in C Language
Loops Used in C Language
When working in C language, we often needs to execute a program for a number of times. In such situations we have to use loops. There are three types of loops used in c language. Loops executes a program section until a certain condition is fulfilled.The execution continuous till the given test condition becomes false. To perform a loop action, we can use any one method of these given below :
Recommended Read: Micro-controller Pin Configuration
Types Of Loops:
  • For loop
  • While loop
  • Do-While loop
For Loop: 
It is most used loop method in C. In For loop, a statement block can be executed for a number of times.In for loop we should have a prior knowledge about "how many times a statement to be executed". The syntax of for loop is as:
Recommended Read: Top Job Requirements of This Year


for(expression1;expression2;expression3)
statement block;


where expression1 is initialize expression.
          expression2 is test condition 
          expression3 is increment/decrement  expression or update.

The example of for loop is given below:

//Program to Print the number 0 to 10
#include<stdio.h>
#include<conio.h>
void main()
{
        int n;
        clrscr();
        for(n=0;n<11;n++)
        printf("%d\n",n);
        getch();
}


This program will print the numbers form 0 to 10 on the screen.
While loop:
In while loop it is not necessary of prior knowledge of "how many times a statement will be executed". We apply this loop in such conditions where we don't know about the number of iterations to be performed. However in some situations a single statement or a group of statements to be repeatedly executed till a particular condition becomes false. The syntax of while loop is given below:
while(condition)
statements;
Now the statements will be executed repeatedly as long as condition is true.
An example of while loop is given below:

syntax of for loop is as:


//Program to find sum of 1st 10 natural numbers
#include<stdio.h.>
#include<conio.h>
void main()
{
      int sum,digit;
      sum=0;
      digit=1;
       while(digit<=10)
{
       sum+ = digit;
       digit++;
}
       printf("The sum is : %d",sum);
       getch();
}



Do-While loop:
The while loop tests the condition and then enters into the body of loop. Some times, we requires to first enter   in loop and then check the condition. In such conditions , we make use of Do-while loop. 
do while first enter in loop and then tests the condition. Mean , in do while loop, statement is executed at least once, whether the condition is true or not. The syntax of do-while loop is given as:
Unlike while loop, do while loop ends with a semi colon.
Recommended Read: Difference B/W Micro controller and Microprocessor


do{
statements;
}while(condition);
An example of do while loop is as following:



//Program to find the sum of even numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int sumx,n;
x=2;
printf("Enter no. of even no, : ");
scanf("%d",&n);
sum=0;
do{
sum = sum+x;
x=x+2;
}
while(x<=n);
printf("The sum is: %d,sum);
getch();
}