Loops in C Language
Loops : The Computer lies in its ability to perform a set of instructions repeatedly. That Means Loop is Number of Times Repeat Until a Particular Condition is Satisfied. This Repetitive Operation is Done Through a Loop Control Statement.
There are Three Methods. They are,
- Using a For Statement
- Using a While Statement
- Using a do-While Statement
First we need to Understanding While Loop in C Program.
While Loop: In This Case programming that you want to do something a fixed number of times. The while loop mostly Cases use for calculate salary of different no.of persons. Convert to temperature from centigrade to Fahrenheit .
Sample Program For While Loop
#include<stdio.h>
int main()
{
int p,n,count;
float r,si;
count=1;
while (count<=3)
{
printf("Enter values p,n,r:\n");
scanf("%d%d%f",&p,&n,&r);
si=p*n*r/100;
printf("Interest = %f",si);
count=count+1;
}
}
For Loop: The For Loop is Most Popular in looping instruction c Programming Language . Why For Loop is Popular ? . For loop Having Three stages Complete at a line.
General Form For Loop Syntax
for ( initial value; test value; increment value )
Sample Program
#include<stdio.h>
int main()
{
int a;
printf("Enter a Values:\n");
scanf("%d",&a);
for (a=a;a<=10;a++)
{
printf("Enter The Value is %d\n",a);
}
}
Nested Loops : The Nested Loop process With in while and for loops in c programming Language.
Sample Program For Nested Loop in For Loop !!
#include<stdio.h>
int main()
{
int r,c,sum;
for(r=1;r<=3;r++) //outer loop
{
for(c=1;c<=2;c++) // inner loop
{
sum=r+c;
printf("r=%d C=%d sum=%d \n",r,c,sum);
}
}
}
Odd Loop : The odd loop using for the far executed the statement with in ten a finite number of times.The odd loop function can be used in below program with do or while methods.
Program For Odd Loop
#include<stdio.h>
int main()
{
char another;
int x;
do{
printf("Enter a Number :");
scanf("%d",&x);
printf("Square of %d Value %d",x,x*x);
printf("\n we want to Enter the Another Number : y/n");
scanf("%c",&another);
}
while(another=='y');
}
Break Statement : The Break Statement is using in loop process jump the out of the loop.
For Example Break Statement in C
// prime number Checking
#include<stdio.h>
int main()
{
int num,i;
printf("Enter number");
scanf("%d",&num);
i=2; // initial value
while( i <=num-1)
{
if(num%i==0)
{
printf("This Number is not Prime ");
break;
}
i++;
}
if(i==num)
printf("prime number");
}
Continue Statement : The Continue Statement is some programming situations. we want to take the control to the beginning of the loop , by passing the statement inside the loop , which have not yet been executed.
Using Keyword " continue ".
Sample Programs for continue
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
continue;
printf("\n %d %d\n",i,j);
}
}
}
do-While Loop : The do-while loop like to be while process.( minor difference ) .The While tests the condition before executing any of the statements with in the while loop. as against this , the do-while tests the condition after having executed the statements with in the loop.
Small Program Example For Do-While Loop
While
#include<stdio.h>
int main()
{
while(4<1)
printf("Welcome");
}
Do -while
#include<stdio.h>
int main()
{
do{
printf("welcome\n");
}
while(4<1);
}
Thanks For Visiting Welcome !!