LOOPING AND BRANCHING STATEMENTS
Loops are used to execute statement or block of statements more than language, it means to do a particular task again and again till a particular condition is true or satisfied.In ‘C’ we have three types of loops. They are known as:
1. While
2. Do while
3. For
In mostly every loop we have to identify 3 main parts of the loop.a) Initialization: starting point of the loop.
b) Condition: ending point of the loop.
c) Increment/decrement: how will we reach from starting to end point of the loop.
1. While:- it executes the block of statements till the value of the conditional expression is true. In while if condition is true, then the body of the loop is executed. After execution of the body , the test condition is again tested and if it is true, the body is executed again. Thi process is repeated till the test is true.
Program to print series 1 to 10 with while loop in c.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=10)
{
printf(“\n%d”,a);
a++;
}
getch();
}
Program to print series 10 to 1 with while loop in c.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
clrscr();
while(a>=1)
{
printf(“\n%d”,a);
a--;
}
getch();
}
Program to print table with while loop in c.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2;
clrscr();
while(a<=20)
{
printf(“\n%d”,a);
a=a+2;
}
getch();
}
Program to print table any number with while loop in c.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,b;
clrscr();
while(a<=10)
{
printf(“\n%d”,a*b);
a++;
}
getch();
}
Write a program to print sum of digits of a number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,s=0;
clrscr();
printf(“enter a no”);
scanf(“%d”,&n); //let n=1234
while(n>0)
{
i=n%10;
s=s+i;
n=n/10;
}
printf(“sum of digits=%d”,s);
getch();
}
Write a program to print reverse of a number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,s=0;
clrscr();
printf(“enter a no”);
scanf(“%d”,&n);
while(n>0)
{
i=n%10;
s=s*10+i;
n=n/10;
}
printf(“reverse of a number=%d”,s);
getch();
}
#include< stdio.h >
#include< conio.h >
void main()
{
int i=1,j=1;
clrscr();
while(i<=5)
{
j=1;
while (j<=5)
{
printf("%d",j);
j++;
}
printf("\n");
i++;
}
getch();
}
Output:=
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,j=1,k=2,n=1,m=1;
clrscr();
while(i<=3)
{
j=1;
while(j<=k)
{
printf(" ");
j++;
}
m=1;
while(m<=n)
{
printf("*");
m++;
}
printf("\n");
k--;
n=n+2;
i++;
}
i=3,k=0,n=3,j=0,m=1;
while(i>1)
{
j=0;
while(j<=k)
{
printf(" ");
j++;
}
m=1;
while(m<=n)
{
printf("*");
m++;
}
printf("\n");
n=n-2;
k++;
i--;
}
getch();
}
#include< stdio.h >
#include< conio.h >
void main()
{
int i=1,j=1;
clrscr();
do
{
j=1;
do
{
printf("%d",j);
j++;
}
while(j<=5);
i++;
printf("\n");
} while(i<=5);
getch();
}
Output:=
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Simple example of for loop in c program:-
Output:-
1 2 3 4 5 6 7 8 9 10
Write a program to find sum 1 to 10 numbers using for loop in c
NESTED FOR LOOP IN C:
Nested loop means nesting or loops means within loop.In nested loop the first loop is called outer loop & second loop is called inner loop. In this the condition of outer loopis true then the condition of inner loop is checked.If it is true then inner loop will execute. The inner loop would act as a part of the body for the outer loop.Therefore,execution of inner loop will depend on the execution of the outer loop.
Write a program to print:-
#include< stdio.h >
#include< conio.h >
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
Output: 12345
12345
12345
12345
12345
Write a program to print:-
#include< stdio.h >
#include< conio.h >
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
Output: 1
12
123
1234
12345
Write a program nested for loop in c:-
#include< stdio.h >
#include< conio.h >
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
Output: 1
22
333
4444
55555
Write a program to print:-
#include< stdio.h >
#include< conio.h >
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
Output: 12345
1234
123
12
1
Branching statements in c
There are another kind of control statements that can change the execution of loop or execution of the program.Branching statements are further divided into three types.
a. Break
b. countinue
c. goto
Break:-
Break statement is used to nexit from the loop or switch statement.Whenever break statement is encountered the rest of the statements inside the loop are ignored and the control go to the next statements after the loop.It can also be used with a while, a do while, a for or a switch statement.
day 7
ReplyDelete