For (Initial Value Condition Incrementation or Decrementation) (Statements )
For (Initial Value Condition Incrementation or Decrementation) (Statements )
A
loop consists of two parts, a body of a loop and a control statement. The control statement is a
combination of some conditions that direct the body of the loop to execute until the specified condition
becomes false. The purpose of the loop is to repeat the same code a number of times.
For loop
A for loop is a more efficient loop structure in 'C' programming. In for loop, first check the condition, if
condition is true then statement is print otherwise exit from the loop.
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
for(n=1;n<=10;n++) //for loop to print 1-10 numbers
{
printf("%d\n",n); //to print the number
}
getch();
}
for (i= 1; i <=5 ; i++)
{
printf(“%d”, i);
}
Example 2
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
for(n=1;n<=10;n+=2)
{
printf("%d\n",n);
}
getch();
}
Example 3 WAP to find even /odd no between 1 to 20.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
for(n=1;n<=20;n++)
{
if(n%2==0)
printf("Even no %d\n",n);
else
printf(“Odd no %d\n”,n);
}
getch();
}
Example 4 : WAP to accept any no from user and find out factorial value .
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,i;
printf(“Enter your no :”);
scanf(“%d”,&n);
m=1;
for(i=1;i<=n;i++)
{
m=m*i;
}
printf(“Factorial value is %d”,m);
getch();
}
Example 5 WAP to accept any number from user and display no is prime no or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,i;
printf(“Enter your no :”);
scanf(“%d”,&n);
for(i=n-1;i>1;i--)
{
m=n%i;
if(m==0)
break;
}
if(m==0)
printf(“Not Prime is %d”,n);
else
printf(“Prime no %d”,n);
getch();
}
Example 6 WAP to generate fibonacci series 1 1 2 3 5 8 13
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i;
a=1;
b=1;
printf(“%d \t %d”,a,b);
for(i=1;i<=5;i++)
{
c=a+b;
printf(“\t %d”,c);
a=b;
b=c;
} getch(); }
Example 7 : WAP to print series
1
2
3
4
5
Sum is 15
#include<stdio.h>
#include<conio.h>
void main()
{
int n ,s=0;;
for(n=1;n<=5;n++)
{
printf("%d\n",n);
s=s+n;
}
printf(“Sum is %d”,s);
getch();
}
While loop
while loops are used in situations where we do not know the exact number of iterations of loop b
eforehand. The loop execution is terminated on the basis of test condition.
In while loop, first check the condition, if condition is true then statement is print otherwise exit from th
e loop.
Syntax
initialization expression;
while (test_expression)
{
// statements
Increment / decrement;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=5)
{
printf(“%d \n”,i);
i++;
}
getch();
}
How it work
i=1;
while(i<=5)
{
printf(“%d\n”,i);
i++;
}
Example 2
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,s=0;
while(i<=5)
{
printf(“%d \n”,i);
s=s+i;
i++;
}
printf(“Sum is %d”,s);
getch();
}
Do – while loop
In do-loop, first statement is print then after increment or decrement values. After increment/decr
ement values then go to check the condition. If condition is true then loop is execute or otherwise
exit from loop.
The body of do...while loop is executed at least once. Only then, the test expression is evaluated.
The main difference between a do-while loop and while loop is in the do-while loop the conditio
n is tested at the end of the loop body, i.e do-while loop is exit controlled whereas the other two l
oops are entry controlled loops.
------------------
do
{
// statements inside the body of the loop
Increment/decrement;
}
while (testExpression);
#include<stdio.h>
#include<conio.h>
void main()
{
int n=1;
do
{
printf(“%d ”,n);
n++;
}
while(n<=5);
}
Example 2
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int k=1,n=0;
6. printf("Enter a number: ");
7. scanf("%d",&n);
8. do
9. {
10. printf("%d \n",(n*k));
11. k++;
12. }
13. while(i<=10);
14. getch();
15. }
Example 3
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n;
a=1;
b=1;
n=1;
printf(“%d \t %d”,a,b);
do
{
c=a+b;
printf(“ \t %d”,c);
a=b;
b=c;
n++;
}
while(n<=5);
getch();
}