Relational
and logical operators in C++ compare two or more operands and return true or
false values based on their results.
Comparison/Relational
Operators:
Comparison
operators are binary operators that evaluate a condition and return 1 if it is
logically true and 0 if it is logically false.
Equal |
a == b |
Not Equal |
a != b |
Greater than or Equal |
a >= b |
Less than or Equal |
a <= b |
Less than |
a < b |
Greater than |
b > a |
Logical
Operators:
To determine
whether an expression is true or false, we employ logical operators. If the
expression is true, it will return 1, but if it is false, it will return 0.
All expressions must be true. |
(a==b) && (c>d) |
Any expression should be true. |
(a==b) || (c>d) |
Not true |
!a |
These
operators are used in decision-making.
if
Code 1 – if
- If you are absent for more than three days, can’t sit the final exam.
#include <iostream>
using namespace std;
int main()
{
//declare variables
int intNoDaysAbsent;
//ask for attendance
cout<<"How many days candidate was absent? ";
//user enters no. of days absent
cin>>intNoDaysAbsent;
if (intNoDaysAbsent>3)
{
cout<<"Candidate has not attend 80% of the
lectures."<<endl;
cout <<"Sorry, candidate is not eligible to sit the
ESE."<<endl;
return 0;
}
cout<<"Candidate is eligible to sit the ESE.";
}
Code 2 – nested if
- If you are absent more than three days and CA Marks less than 50, can’t sit the final exam.
#include <iostream>
using namespace std;
int main()
{
const float fltCARatio = 0.3;
//declare variables
int intCAMarks = 0;//store Continuous Assessment marks (out of 100)
int intNoDaysAbsent;
//ask for attendance
cout<<"How many days candidate was absent? ";
//user enters no. of days absent
cin>>intNoDaysAbsent;
if (intNoDaysAbsent<=3)
{
cout<<"Enter candidate's CA marks (out of 100) :";
cin>>intCAMarks;
if (intCAMarks<0 ||
intCAMarks>100)
{
cout<<"Error: CA
marks should be between 0 - 100."<<endl;
}
else
if(intCAMarks<50)
{
cout<<"Candidate has not got 50% of the CA
marks"<<endl;
cout<<"Sorry, candidate is not eligible to sit theESE."<<endl;
}
else
cout<<"Candidate is eligible to sit
the ESE."<<endl;
}
else{
cout<<"Candidate has not attend 80% of the
lectures."<<endl;
cout <<"Sorry, candidate is not eligible to sit the
ESE."<<endl;
}
}
Code 3 – else if
- Finally, calculate the final grade of the subject.
#include <iostream>
using namespace std;
int main()
{
//declare constants
const float fltCARatio = 0.3;
//declare variables
int intCAMarks = 0;//store Continuous Assessment marks (out of 100)
int intESEMarks = 0;
int intNoDaysAbsent;
float finalMark;
//ask for attendance
cout<<"How many days candidate was absent? ";
//user enters no. of days absent
cin>>intNoDaysAbsent;
if (intNoDaysAbsent<=3)
{
cout<<"Enter candidate's CA marks (out of 100) :";
cin>>intCAMarks;
if (intCAMarks<0 ||
intCAMarks>100)
{
cout<<"Error: CA
marks should be between 0 - 100."<<endl;
}
else
if(intCAMarks<50)
{
cout<<"Candidate has not got 50% of the CA
marks"<<endl;
cout<<"Sorry, candidate is not eligible to sit theESE."<<endl;
}
else
cout<<"Candidate is eligible to sit
the ESE."<<endl;
}
else{
cout<<"Candidate has not attend 80% of the
lectures."<<endl;
cout <<"Sorry, candidate is not eligible to sit the
ESE."<<endl;
}
cout<<"Enter candidate's ESE marks (out of 100): ";
cin>>intESEMarks;
finalMark = (intESEMarks*(1-fltCARatio) + intCAMarks*fltCARatio) ;
if (finalMark>75)
cout<<"Your
Grade is : A"<<endl;
else if (finalMark>65)
cout<<"Your
Grade is :B"<<endl;
else if (finalMark>55)
cout<<"Your
Grade is : C"<<endl;
else if (finalMark>35)
cout<<"Your
Grade is : S"<<endl;
else
cout<<"Your
Grade is : F"<<endl;
}
Post a Comment