Loops in C
When we use a block of statement to perform set of instructions repeatedly, fixed time or until the given particular condition is being satisfied.
There are three types of loops available in c programming.
While loop
Do While loop
For loop
While loop:
In while loop a block statement will execute repeatedly until the condition is true.
while is a keyword used before condition , the general syntax is as follows:
Initialization of condition variable;
while(condition)
{
Statement; Expression;
increment/decrement variable;
}
To initialization of condition variable we have to declare a starting values of variables, then while Condition is checked for loop execution, if the condition is true body of loop will be executed.
No semicolon allowed at the end of condition otherwise loop will not work.
The body of loop contain Statement, Expression and Increment and decrement variables.
A statement may contains any statement, any expression and c function like printf() and scanf(), that executed until the condition is true and every statement is end with the semicolon.
To update value of condition compulsory we need to provide increment and decrement variable.
The statements written in the body of loop will execute until condition becomes false.
Flow chart of While loop
Example of While loop
#include<stdio.h>
#include<conio.h>
void main()
{
int n=0;
clrscr();
while(n<=10)
{
printf("\n%d\n",n);
n++;
}
getch();
}
Do While Loop
Do while have same functionality as while loop .
Do and while is a keyword used, the general syntax is as follows:
Initialization of condition variable;
do
{
Statement; Expression;
increment/decrement variable;
}
while(condition);
To initialization of condition variable we have to declare a starting values of variables, then while Condition is checked for loop execution, if the condition is true body of do will be executed. semicolon required at the end of condition otherwise loop will not work.
The body of do contain Statement, Expression and Increment and decrement variables.
A statement may contains any statement, any expression and c function like printf() and scanf(), that executed until the condition is true and every statement is end with the semicolon.
To update value of condition compulsory we need to provide increment and decrement variable.
The statements written in the body of loop will execute until condition becomes false.
Example of While loop
#include<stdio.h>
#include<conio.h>
void main()
{
int n=0;
clrscr();
do
{
printf("\t%d\n",n);
n++;
}
while(n<=10);
getch();
}
Difference between while and do-while loops
While loop
Keyword – while is used
Condition written and checked before statements
At beginning If condition is false loop will not execute
No semicolon(;) allowed after condition
Do-while loop
Keywords – do and while
Condition written and checked after body of do statements
At least one iteration of loop statements will be executed as condition is written after body of do.
Semicolon (;)is required after condition.
For Loop:
For loop have same functionality as while loop and do while loop, only difference is that initialization of condition variable, condition and increment and decrement is written in single line. for is keywords us used, the general syntax is as follows:
for(initialization;condition;increment/decrement)
{
Statement;
Expression;
}
Initialization of condition variable, condition and increment and decrement are separated using semicolon but no semicolon allowed after increment/Decrement.
To initialization of condition variable we have to declare a starting values of variables, then while Condition is checked for loop execution, if the condition is true body of loop will be executed.
The body of loop contain Statement, Expression and Increment and decrement variables.
A statement may contains any statement, any expression and c function like printf() and scanf(), that executed until the condition is true and every statement is end with the semicolon.
To update value of condition compulsory we need to provide increment and decrement variable.
The statements written in the body of loop will execute until condition becomes false.
Flow chart of For Loop
Example of For Loop
/*Program to show use of for loop */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
for(n=0;n<=10;++n)
{
printf("%d\n",n);
}
getch();
}
Nested for loop
When we declare the for loop inside the body of another for loop it is called as nested for loop. The loop present inner side is called as inner for loop and the loop outer side loop is called as outer for loop. The general syntax is as follows:
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
Statement;
Expression;
}
}
Example of Nested For loop
#include<stdio.h>
void main()
{
int a,b,sum;
for(a=1;a<=2;a++)/*outer loop*/
{
for(b=1;b<=2;b++)/*inner loop*/
{
sum=a+b;
printf(“a=%d c=%d sum=%d\n”,a,b,sum);
}
}
}