Else if Statement in C language
The Else if statement is used to execute one code from multiple conditions. It is also called multipath decision statement. It is a chain of if…else statements in which each if statement is associated with else if statement and last would be an else statement.
Syntax:-
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
…
Else
{
//code to be executed if all the conditions are false
}
Example 1,
Write a C program to find the grade of the student
#include <stdio.h>
#include <conio.h>
void main ()
{
int average;
printf(“Enter average of student : “);
scanf(“%d”,&average);
if(average <= 50)
{
printf(“nGrade D”);
}
else if (average <= 60)
{
printf(“nGrade C”);
}
else if (average <= 70)
{
printf(“nGrade B”);
}
else
{
printf(“nGrade A”);
}
getch();
}
Example 2,
C Program of Calculator Using If-else Statement
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,d,e,n;
printf(“Enter the two no:-“);
scanf(“%d%d”,&a,&b);
// scanf(“%d”,&b);
c=a+b;
d=a-b;
e=a*b;
printf(“Press 1 for additon”);
printf(“nPress 2 for subtraction”);
printf(“nPress 3 for multiply”);
printf(“nnEnter your choice”);
scanf(“%d”,&n);
if(n==1)
{
printf(“The addition is:%d”,c);
}
else if(n==2)
{
printf(“The subtration is:%d”,d);
}
else if(n==3)
{
printf(“The mul is:%d”,e);
}
else
{
printf(“please input proper operator”);
}
getch();
}