0% found this document useful (0 votes)
43 views7 pages

For (Initial Value Condition Incrementation or Decrementation) (Statements )

The document discusses different types of loops in C programming language - while loop, do-while loop, and for loop. The for loop is considered more efficient as it allows initializing a counter, checking a condition, and incrementing/decrementing the counter in one place. Examples are provided to demonstrate the use of each loop type to print numbers, find factors, calculate factorials, check for prime numbers, generate Fibonacci series, and print series with running sum. While and do-while loops are used when the number of iterations is unknown beforehand, while for loop is preferred when the number of iterations is fixed and known in advance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views7 pages

For (Initial Value Condition Incrementation or Decrementation) (Statements )

The document discusses different types of loops in C programming language - while loop, do-while loop, and for loop. The for loop is considered more efficient as it allows initializing a counter, checking a condition, and incrementing/decrementing the counter in one place. Examples are provided to demonstrate the use of each loop type to print numbers, find factors, calculate factorials, check for prime numbers, generate Fibonacci series, and print series with running sum. While and do-while loops are used when the number of iterations is unknown beforehand, while for loop is preferred when the number of iterations is fixed and known in advance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

A Loop executes the sequence of statements many times until the stated condition becomes false.

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.

'C' programming language provides us with three types of loop constructs:

1. The while loop

2. The do-while loop

3. The for loop

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.

The general structure of for loop is as follows:

for (initial value; condition; incrementation or decrementation )


{
statements;
}
The initial value of the for loop is performed only once.
The condition is a Boolean expression that tests and compares the counter to a fixed value after each
iteration, stopping the for loop when false is returned.
The incrementation/decrementation increases (or decreases) the counter by a set value.

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();
}

You might also like