 It is used to change the order of the program
based on condition.
 Categories:
› Sequential structure
› Selection structure
› Iteration structure etc,.
 Sequential structure
› In which instructions are executed in sequence.
 Selection structure
› In which instruction are executed based on the result
of some condition.
 Iteration structure
› In which instruction are executed repeatedly.
 It allows the program to make a choice
from alternative paths.
 C provide the following selection
structures
› IF statement
› IF … ELSE statement
› Nested IF … ELSE statement
› IF … ELSE ladder
Syntax:
if (condition is true)
{
Statements;
}
False
True
If
condition
Statements
#include<stdio.h>
#include <conio.h>
void main( )
{
int a;
clrscr( );
printf("nEnter the number:");
scanf("%d",&a);
if(a>10)
{
printf(" n a is greater than 10");
}
getch( );
}
Output:
Enter the number: 12
a is greater than 10
Syntax
if (condition)
{
True statements;
}
else
{
False statements;
}
If
Condition
True False
True
statements
False
statements
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a;
clrscr( );
printf("nEnter the number:");
scanf("%d",&a);
if(a>10)
{
printf(" n a is greater than 10");
}
else
{
printf(" n a is less than 10");
}
getch( );
}
Output:
Enter the number: 16
a is greater than 10
Example
If
Condition
2
True False
True
statements
False
statements
If
Condition
1
False
Statements
True
False
Syntax
if (condition1)
{
if (condition2)
{
True statements;
}
else
{
False statements;
}
}
else
{
False statements;
}
Condition
1
Statements
Condition
2
Statements
Condition
3
Statements Statements
TRUE
TRUE
TRUE FALSE
FALSE
FALSE
Syntax
IF (condition1)
{
statements;
}
else if (condition2)
{
statements;
}
else if (condition3)
{
statements;
}
else
{
statements;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3;
float avg;
printf("nEnter the marks:");
scanf("%d%d%d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf("n The average is:%f",avg);
printf("n The Grade is:");
if(avg>=60)
{
printf("First class");
}
else if(avg>=50)
{
printf("Second class");
}
else if(avg>=35)
{
printf("Thrid class");
}
else
{
printf("Fail");
}
getch();
}
Output:
Enter the marks:65
75
70
The average is:70.000000
The Grade is: First class
#include<stdio.h>
#include<conio.h>
void main()
{
float Basic;
float Gross_salary,da,hra,pf;
Gross_salary=da=hra=pf=0;
clrscr();
printf("nEnter the salary:");
scanf("%f",&Basic);
hra=Basic*0.10;
pf=Basic*0.12;
da=Basic*0.35;
Gross_salary=Basic+da+hra-pf;
printf("n The Gross salary is:%f",Gross_salary);
if(Gross_salary>=35000)
{
printf("nThe Grosssalary is Greater than 35000");
}
else if(Gross_salary>=25000)
{
printf("nThe Grosssalary is Between 25000 and 35000");
}
else
{
printf(“nThe Gross salary is less than 25000");
}
getch();
}
Output:
Enter the Basic:12000
The Gross salary is 15960.000000
The Gross salary is less than 25000
 It is used to execute some instructions
several time based on some condition.
› WHILE
› Do…WHILE
› For
Syntax
.
WHILE (condition)
{
.
Body of the loop;
.
}
Body of The loop
condition
False
True
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
Printf(“enter the value of n”);
Scanf(“%d”,&n)
while(i<=n)
Printf(“hellon”);
i++;
}
Output:
Enter the Number:3
Hello
Hello
Hello
Syntax
do
{
Body of the loop
}while (condition);
Body of The loop
condition
False
True
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
printf("nEnter the Number:");
scanf("%d",&n);
do
{
printf(“hellon”);
i++;
}while(i<=n);
}
Output:
Enter the Number: 4
Hello
Hello
Hello
Hello
Syntax
for (initialization; cond; Inc/Dec)
{
Body of the loop
}
Initialization
condition
False
Body of the loop
Inc / Decrement
True
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
Printf(“enter the value of n”);
Scanf(“%d”,&n);
For(i=0;i<=n;i++)
{
Printf(“Hellon”);
}
Output:
Enter the Number:3
Hello
Hello
Hello
Hello
Syntax:
for (initi; cond; Inc/Dec)
{
for (initi; cond; Inc/Dec)
{
Body of the loop
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
printf("n");
for(j=1;j<=3;j++)
{
printf("%dt",j);
}
}
getch();
}
Output:
1 2 3
1 2 3
1 2 3
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=4;i++)
{
printf("n");
for(j=i;j<=4;j++)
{
printf("%dt",j);
}
}
getch();
}
Output:
1 2 3 4
2 3 4
3 4
4
Syntax
switch (expression)
{
case constant 1:
block1;
break;
case constant 2:
block2;
break;
.
.
default :
default block;
break;
}
Case 1
Case 2
Default
case
Switch
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("nEnter the Number:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("n Its in case 1");
break;
case 2:
printf("n Its in case 2");
break;
default:
printf("n Its in default");
break;
}
getch();
}
Output:
Enter the Number:1
Its in case 1
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,b,c;
clrscr();
printf("nenter the two numbers:");
scanf("%dt%d",&a,&b);
printf("nEnter the choice: ");
scanf("%d",&n);
switch(n)
{
case 1 :
c=a+b;
printf("The addition is :%d",c);
break;
case 2 :
c=a-b;
printf("The sub is :%d",c);
break;
default:
printf(“n no operation”);
break;
}
getch();
}
Try to find the output youself!
Example 2
 It is used to terminate the loop
 When a break statement is encountered inside a loop,
then the loop is terminated.
Loops with break Statement
while(cond)
{
…………
if(cond)
break;
…………
}
do
{
…………
if(cond)
break;
…………
} while(cond);
for (initialization; condition; Inc/Dec)
{
…………
if(cond)
break;
…………
}
while(cond)
{
…………
if(cond)
continue;
…………
}
do
{
…………
if(cond)
continue;
…………
} while(cond);
for (initi; condt; Inc/Dec)
{
…………
if(cond)
continue;
…………
}
For Loop with Continue Statement
label:
…………
…………
…………
goto label;
…………
goto label;
…………
…………
…………
label:
…………
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n;
clrscr();
printf("nEnter the value of a,b:");
scanf("%d%d",&a,&b);
printf("nMENU");
printf("n1.ADDn2.SUBn3.MULTIPLYn0.EXIT");
printf("nEnter the choice:");
scanf("%d",&n);
switch(n)
{
case 1:
c=a+b;
printf("nThe result of Addition is:%d",c);
break;
case 2:
c=a-b;
printf("nThe result of Subtraction is:%d",c);
break;
case 3:
c=a*b;
printf("nThe result of Multiplication is:%d",c);
break;
case 0:
exit(0);
break;
}
getch();
}
Enter the value of a,b:5
6
MENU
1.ADD
2.SUB
3.MULTIPLY
0.EXIT
Enter the choice:1
The result of Addition is:11
An Armstrong number of three digits is an integer such
that the sum of the cubes of its digits is equal to the
number itself. For example, 371 is an Armstrong
number since 3**3 + 7**3 + 1**3 = 371.
#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n,a;
printf("nEnter the number:");
scanf("%d",&n);
a=n;
while(n>0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(a==sum)
{
printf("nIt is an Armstrong number");
}
else
{
printf("nIt is not an Armstrong number");
}
getch();
}
Enter the number:153
It is an Armstrong number
#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n;
printf("nEnter the no:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("sum of the digits is:%d",sum);
}
Enter the no:156
sum of the digits is:12
#include<stdio.h>
#include<conio.h>
void main()
{
int r=0,sum=0,n;
printf("nEnter the no:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf("Reverse of the number is:%d",sum);
getch();
}
Enter the no:567
Reverse of the number is:765
#include<stdio.h>
#include<conio.h>
void main()
{
int f=0,f1=-1,f2=1,n,i;
printf("nEnter the number:");
scanf("%d",&n);
while(f<n)
{
f=f1+f2;
f1=f2;
f2=f;
printf("t%d",f);
}
getch();
}
Enter the number:5
0 1 1 2 3 5
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a,b,c;
clrscr( );
printf(" nEnter the value of a:");
scanf("%d",&a);
printf(" nEnter the value of b:");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf(" nThe value of a is:%d",a);
printf(" nThe value of b is:%d",b);
getch( );
}
Enter the value of a:5
Enter the value of b:4
The value of a is:4
The value of b is:5
Output of Example 6
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a,b;
clrscr( );
printf(" nEnter the value of a:");
scanf("%d",&a);
printf(" nEnter the value of b:");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf(" nThe value of a is:%d",a);
printf(" nThe value of b is:%d",b);
getch( );
}
Enter the value of a:5
Enter the value of b:6
The value of a is:6
The value of b is:5
Output of Example 7
#include<stdio.h>
#include <conio.h>
#include<math.h>
void main ( )
{
int a,b,c,d,r1,r2;
clrscr( );
printf(" nEnter the value of a:");
scanf("%d",&a);
printf(" nEnter the value of b:");
scanf("%d",&b);
printf(" nEnter the value of c:");
scanf("%d",&c);
d=b*b-4*a*c;
if(d>=0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf(" nThe roots are %d,%d",r1,r2);
}
else
{
printf(" nThe roots are imaginary");
}
getch( );
}
Enter the value of a:4
Enter the value of b:5
Enter the value of c:6
The roots are imaginary

Decision making and branching