C++ Tutorials

C++ Tutorials

Relational operators are used to compare the value of two variables.

Operators

Operator Use
== Check value of 2 variable are equal
!= Check value of 2 variable are equal or not
> check is value is greater than
>= check is value is greater than or equal to
< check value is less than
<= check value is less than or equal to

Example :


#include <iostream>
using namespace std;
int main()
{
    int a = 21;
    int b = 10;
    int c ;

    // == operator
    if( a == b )
    {
        cout<<"a is equal to b\n";
    }
    else
    {
        cout<<"a is not equal to b\n";
    }

    // < operator
    if ( a < b )
    {
        cout<<"a is less than b\n";
    }
    else
    {
        cout<<"a is not less than b\n";
    }

    // > operator
    if ( a > b )
    {
        cout<<"a is greater than b\n";
    }
    else
    {
        cout<<"a is not greater than b\n";
    }

    // Lets change value of a and b
    a = 5;
    b = 20;
    // <= operator
    if ( a <= b )
    {
    cout<<"a is either less than or equal to  b\n";
    }


    // >= operator
    if ( b >= a )
    {
        cout<<"b is either greater than  or equal to b\n";
    }

    return 0;
}

Output :

  a is not equal to b
  a is not less than b
  a is greater than b
  a is either less than or equal to  b
  b is either greater than  or equal to b