General Introduction:
Loop is a set of program statement that executes repeatedly and conditionally. That is the block of statement is executed for certain number of time until a condition is satisfied.
When an identical task is to be performed several times, then a loop is used. A loop consists of two segments: a condition statement and the loop body. The loop body is executed until the condition statement becomes false.
There are three types of loop in C. They are:
a) for loop
b) while loop
c) do while loop
a) For Loop:
For loop is a definite loop and is useful when we know the number of repetition required in the loop body in advance. It is an entry control loop i.e. condition is checked first and then loop body is executed only if it is true.
Syntax of for loop:
for(initialization;condition;increment/decrement)
{
statement;
}
Flowchart of for loop:
Semantic of for loop:
When a for loop is encountered, it first executes the initialization where the value of loop controlling value will be initialized. This expression is executed only once within a for loop. Then the condition is checked. If the condition is true, the body of the loop will be executed, otherwise the control transfer out of the for loop.
When a condition is true, it executes the loop body and the control transfer to increment/ decrement where the value of loop controlling variable will be updated and again tested.
The loop body will be executed repeatedly until the condition becomes false.
Also Read: C Program to Check if a Number is Prime or Composite
Example of Implementation of for loop in C:
/* Program to find the sum of first n natural numbers using for loop*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
int sum=0;
int n;
printf("Enter the nth term to which you want to find the natural numbers: ");
scanf("%d",&n);
/*for loop*/
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("\nThe sum to %dth term of natural number is :%d",n,sum);
getch();
return(0);
}
Output:
b) While loop:
While loop is an indefinite loop. It is an entry controlled loop where the condition is tested before entering the loop body.
Syntax of while loop:
while(condition)
{
statement;
}
Flowchart of while loop:
Semantic of while loop:
While a while loop is encountered, it first checks the condition. If it is true then the body of the loop will be executed. After executing the body, the condition is checked again. If it is true, then again the loop body will be executed.
This process continues until the condition becomes false. Then the control transfers out of the loop.
Example of Implementation of while loop in C:
/* Program to find the sum of first n natural numbers using while loop*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
int sum=0;
int n;
printf("Enter the nth term to which you want to find the natural numbers: ");
scanf("%d",&n);
/*while loop*/
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\nThe sum to %dth term of natural number is :%d",n,sum);
getch();
return(0);
}
Output:
c) Do while Loop:
It is also an indefinite loop. Do while loop is an exit controlled loop where the body of loop is executed first and only then the condition will be evaluated. The loop executes continuously till the condition is true.
Syntax of do while loop:
do
{
statement;
}while(condition);
Flowchart of do while loop:
Semantic of do while loop:
When a do while loop is encountered, it first executes the loop body and then evaluates the condition. If it is true, again the loop body is executed and the condition is checked again. This continues until the condition becomes false i.e if the condition is false, then control transfers out of the do while loop.
Example of Implementation of do while loop in C:
/* Program to find the sum of first n natural numbers using do while loop*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
int sum=0;
int n;
printf("Enter the nth term to which you want to find the natural numbers: ");
scanf("%d",&n);
/* do while loop*/
i=1;
do
{
sum=sum+i;
i++;
} while(i<=n);
printf("\nThe sum to %dth term of natural number is :%d",n,sum);
getch();
return(0);
}