The ‘if-else if’ statement is an extension of the “if-else” conditional branching statement. When the expression in the “if’ condition is “false” another “if-else” construct is used to execute a set of statements based on the expression.
Syntax:
if(expression)
{ statements }
else if(expression)
{ statements }
else if(expression)
{ statements }\
Example: for ‘if else if’ statement
#include<iostream.h>
int main (void)
{
int per;
cout<<"Enter your Percentage::";
cin>>per;
If(per>=80)
{
cout<<"passed with Distinction"<<endI;
}
else if(per>=60)
{
cout <<"First Class"<<end1;
}
else if(per>=50)
}
cout<<"Second Class"<<end1;
}
else
{
cout<<"Third Class"<<end1;
}
return 0;
}
Result:
Enter your Percentage::60
First Class
In the above example, the ‘if else if’ statement is used to check multiple conditions based on the percentage of marks got, as input. The user entered percentage as 60, then first condition evaluated and returned with false. This resulted in checking for second if condition and resulted in true which give the print “first class”.