Loops C Programing
Loops C Programing
C programming
AL SO K NO WN AS I TE R AT IO N S TAT EM EN TS
Introduction and Uses
A loop statement allows us to execute a statement or group of
statements multiple times. Given below is the general form of a loop
statement in most of the programming languages
Example:
If you want to print a series of number, you can do it manually too. But
if the series is very big like all the even numbers upto 1000, you might
want to use loops for your help.
For Loop
A For loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times. It is the
simplest Looping statement in C.
Syntax:
for ( Initialization exp.; Test exp.; Update exp.)
{
statement(s);
}
Initialization
Test
Condition
If true If False
Body
Update
Exit
While Loop
A While loop in C programming repeatedly executes a target statement
as long as a given condition is true. Its similar to For loop, just the exp.
(s) are written in different lines.
Syntax:
while( condition );
{
statement(s);
}
Initialization
Test
Condition
If true If False
Body of the
loop and
update exp.
Exit
D0-While loop
Unlike For and while loops, which test the loop condition at the top of
the loop, the do...while loop in C programming checks its condition at
the bottom of the loop.
A Do...while loop is similar to a while loop, except the fact that it is
guaranteed to execute at least one time.
Syntax:
do {
statement(s);
} while( condition );
Initialization
Body of the
loop and
update exp.
If true
Test
Condition
If False
Exit
Let’s See Some
Programs
Program to print series
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr(); 1/1 - 2/4 + 3/9 - 4/16 +
for(int i=1;i<=10;i++)
{ 5/25 - 6/36 + 7/49 - 8/64 +
if(i%2==0)
printf("%d/%d + ",i,i*i);
else
9/81 - 10/100 +
printf("%d/%d - ",i,i*i);
}
getch();
}
Void main()
{
Binary to decimal converter
int num=1001;
rem= 1001%10=1 rem= 100%10=0
int dec=0,rem,base=1;
printf("Enter bcd number:"); dec=0+1*1=1 dec=1+0*2=1
scanf("%d",&num); num= 1001/10=100 num= 100/10=10
while(num>0) base= 1*2=2 base= 2*2=4
{
rem=num%10;
rem=10%10=0 rem=1%10=1
dec=dec+rem*base;
num=num/10; dec=1+0*4=1 dec=1+1*8=9
base=base*2; num=10/10=1 num=1/10=0
} base=4*2=8 base=8*2=16
printf(“%d”,dec);
getch();
} dec= 9
int i,j,k,l,n=4;
for(i=1;i<=n;i++)
{
Number triangle
for(j=n;j>i;j--)
{ _ _ _ 1
printf(" ");
}
for(k=1;k<=i;k++)
_ _ 1 2 1
{
printf("%d ",k);
_ 1 2 3 2 1
}
for(l=i-1;l>=1;l--)
1 2 3 4 3 2 1
{
printf("%d ",l);
}
printf("\n");
}
int i,j,k,x,n=3;
for(i=0;i<=n;i++)
_ _ _ 1 Pascal’s triangle
{
int c=1;
for(k=n;k>i;k--) _ _ 1 1
{
printf(" ");
}
for(j=0;j<=i;j++)
_ 1 2 1
{
printf("%d ",c);
c=(c*(i-j)/(j+1)); 1 3 3 1
}
printf("\n");
}
Thank-you Very Much
Any Questions? 🙂
A presentation by:
Akshansh kumar
Anmol Sharma
Divyansh Gupta
Madhav Maloo
Shivansh Seth
BCA 1A