In: Computer Science
Write a C++ program that uses all the relational operators.
An operator is a symbol that is used to perform some manipulations with the values
There are several types of operators:
Arithematic operator
Relational operator
Bitwise operator ,etc
Relational operators are the operators that are used to compare two values.
Syntax:
value1 operator value2
Here , value1 and value2 can be of string , integer or any data type
operator used as part of relational operations are :
< : Less than operator - Checks if left value is less than right value , then the result is true otherwsie false
Example: 5<7 -> true , if 7<5 -> false
> : Greater than operator - Checks if left value is greater than right value , then the result is true otherwsie false
Example: 7>5 -> true , if 5>7 -> false
== : Equal to - Checks if left value is equal to right value , then the result is true otherwsie false
Example: 7==7 -> true , if 5==7 -> false
<= : Less than Equal to operator - Checks if left value is less than or equal to right value , then the result is true otherwsie false
Example: 5<=7 -> true , if 7<=7 -> true , if 7<=5 -> false
>= : Greater than Equal to operator - Checks if left value is greater than or equal to right value , then the result is true otherwsie false
Example: 7>=5 -> true , if 7>=7 -> true , if 5>=7 -> false
!= : Not equal to - Checks if left value is not equal to right value , then the result is true otherwsie false
Example: 5!=7 -> true , if 7!=7 -> false
Precedence of operators:
>= , > , <= ,< - Higher precedence operators
== , != - Lower precedence operators
Meaning , if an expression is having both high and low precedence operator , then higher precedence operators will be evaluated first and low precedence will be evaluated after that
\n -> is a formatting character used for new line . It is used to print ouput in new line.
Code in C++ :
#include <iostream>
using namespace std; //used for input output streams to printoutput /take input in console
int main()
{
cout<<"Relation operators example : "<<"\n";
int value1= 100;
int value2= 70;
//Not Equal to opertor usage
if(value1 != value2){
cout<<"Value1 and Value2 are not equal "<<"\n";
}
else{
cout<<"Value1 and Value2 are equal "<<"\n";
}
//Equal to opertor usage
if(value1 == value2){
cout<<"Value1 and Value2 are equal "<<"\n";
}
else{
cout<<"Value1 and Value2 are not equal "<<"\n";
}
//less than opertor usage
if(value1 < value2){
cout<<"Value1 is less than Value2 "<<"\n";
}
else{
cout<<"Value1 is not less than Value2 "<<"\n";
}
//Greater than opertor usage
if(value1 > value2){
cout<<"Value1 is greater than Value2 "<<"\n";
}
else{
cout<<"Value1 is not greater than Value2 "<<"\n";
}
//value change of value2 variable
value2= 100;
//less than Equal to opertor usage
if(value1 <= value2){
cout<<"Value1 is less than or equal to Value2
"<<"\n";
}
else{
cout<<"Value1 is not less than or equal to Value2 ";
}
//Greater than Equal to opertor usage
if(value1 >= value2){
cout<<"Value1 is greater than or equal to Value2 ";
}
else{
cout<<"Value1 is not greater than or equal to Value2 ";
}
}
OUTPUT: