C++ Relational Operators –

In Relational Operators, relational refers to the relationships that values or operands can have with one another arn are used to determine the relation among different operands.

Types of Relational Operators

C++ provides 6 types of Relational operators for comparing numbers and characters –

relational operators

Truth Table of Relational Operators

The relational expression will gives boolean value 1, if the comparison between the two operands is true,
otherwise, it will give boolean value 0 . For example,

M N M<N M<=Q M==Q M>Q M>=Q M!=Q
0 1 1 1 0 0 0 1
1 0 0 0 0 1 1 1
3 3 0 1 1 0 1 0
2 6 1 1 0 0 0 1

Example Of Relational Operators :

Following table summarizes of relational operators in C++

S.no Operators Name Example  Description
1 > Greater Than 6>5 =0
15>6 =1
Returns Boolean truth value(0 for false and 1 for true)
2 < Less than 6<5 =0
5<6 =1
Returns Boolean truth value
3 Greater than equal to 6≥5 = 1
5≥6 = 0
Returns Boolean truth value
4 Less than equal to 6≤5 =0
5≤6 =1
Returns Boolean truth value
5 == Equality 6==5 =0
6==6 =1
Returns Boolean truth value
6 != Not equal to 6!=5 =1
6!=6 =0
Returns Boolean truth value

Some Important Notes about Relational Operators :

 * (=) & (==) operator - The (=) operator is used to assign the values and   therefore called as assignment operator. The (==) operator is used for    comparing the (LHS) and (RHS) values.

 *Avoid equality comparisons on floating point numbers - because they are 
  not as exact as integer arithmetic

* Avoid comparing signed and unsigned values- as the compiler will treat 
  signed value as unsigned. If signed value is negative, it will be 
  treated as unsigned value. 
  For example, a=2 , b=-6 ; If we have the expression (a<b) 
  then, the expected result will be 0 (boolean zero), But the produced 
  result will be 1 (boolean one).
  
  Solution for comparing signed and unsigned values : Explicit Type 
  casting (conversion of data type to another one explicitly) - The above 
  expression can be cast as follows :
         ((signed int) a < b )  Produced result 0 .

Example of C++ program for Relational Operators –

#include<iostream.h>
int main()
    {
    int x, y;
    cout<<"enter the value of x and y = \n"
    if (x == y)
        cout<<"\n x is equal to y");
    else if (x > y)
        cout<<"\n x is greater than y ");
    else
        cout<<"\n x is smaller than y ");
    return 0;
    }

Output Examples:

Enter The value of x and y=     //1st example
40
30
x is greater than y
Enter The value of x and y=    //2nd Example
10
30
x is smaller than y
Enter The value of x and y=   //3rd example
30
30
x is equal to y
     

Incoming search terms:

  • relational operator program of integer of c

Leave a Reply