IF ELSE IF in C++

 IF ELSE IF

If there are more than three alternatives & indentation is not consistent ,it may be difficult to determine the logical structure of the if statement. in such situations, if statement with multiples alternatives (if-else if)
can be a good option.
The general format of if else if is:

if(condition)
    statement;
else if(condition)
    statement;
else if(condition)
    statement;
else    
    statement;

Program:

#include <iostream>
using namespace std;
void main()
{
float pf,iict,cs,ds,percent,ob_marks=0.0; //variable initializaton & declaration...
cout<<"Enter Obtained Marks of 'PF':"; //show msgs & take input...
cin>>pf;
cout<<"Enter Obtained Marks of 'IICT':";
cin>>iict;
cout<<"Enter Obtained Marks of 'DS':";
cin>>ds;
cout<<"Enter Obtained marks of 'Communication Skills':";
cin>>cs;
ob_marks=pf+iict+cs+ds;
percent=(ob_marks/400)*100;
cout<<"Percentage:"<<percent<<endl;
if(percent>=90 && percent<=100)
cout<<"Grade:A+"<<endl;
else if(percent>=80 && percent<=89)
cout<<"Grade:A"<<endl;
else if(percent>=70 && percent<=79)
cout<<"Grade:B"<<endl;
else if(percent>=60 && percent<=69)
cout<<"Grade:C"<<endl;
else if(percent>=50 && percent<=59)
cout<<"Grade:D"<<endl;
else if(percent>=0 && percent<=40)
cout<<"Grade:F"<<endl;
else
cout<<"Invalid Input";
system("pause");
}





Comments

Popular Posts