Related Articles
























































Relational Operators In C/C++

Prashant | Sun, 02 Aug, 2020 | 101

Relational Operators in C/CPP

Relational operators are used to compare two operands; these are the binary operators that require two operands. These operators compare both operands and returns either 1 (true) or 0 (false).

Type of the relational operators

The relational operators are,

SNo.OperatorDescriptionExample
1==Equal To operatorx == y
2!=Not Equal To operatorx != y
3<Less Than operatorx < y
4<=Less Than or Equal To operatorx <= y
5>Greater Than operatorx > y
6>=Greater Than or Equal To operatorx >= y

1) Equal To operator (==)

Equal To (==) operator compares both operands and returns 1 if both are the same; 0, otherwise.

Syntax:

operand1 == operand2

Example:

// C++ program to demonstrate the
// example of == operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;

    cout << "(x == y) : " << (x == y) << endl;

    if (x == y)
        cout << x << " is equal to " << y << endl;
    else
        cout << x << " is not equal to " << y << endl;

    x = 20;
    y = 30;

    cout << "(x == y) : " << (x == y) << endl;

    if (x == y)
        cout << x << " is equal to " << y << endl;
    else
        cout << x << " is not equal to " << y << endl;

    return 0;
}

Output:

(x == y) : 1
10 is equal to 10
(x == y) : 0
20 is not equal to 30

2) Not Equal To operator (!=)

Not Equal To (!=) operator compares both operands and returns 1 if both are not the same; 0, otherwise.

Syntax:

operand1 != operand2

Example:

// C++ program to demonstrate the
// example of != operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;

    cout << "(x != y) : " << (x != y) << endl;

    if (x != y)
        cout << x << " is not equal to " << y << endl;
    else
        cout << x << " is equal to " << y << endl;

    x = 20;
    y = 30;

    cout << "(x != y) : " << (x != y) << endl;

    if (x != y)
        cout << x << " is not equal to " << y << endl;
    else
        cout << x << " is equal to " << y << endl;

    return 0;
}

Output:

(x != y) : 0
10 is equal to 10
(x != y) : 1
20 is not equal to 30

3) Less Than operator (<)

Less Than (<) operator compares both operands and returns 1 if the first operand is less than the second operand; 0, otherwise.

Syntax:

operand1 < operand2

Example:

// C++ program to demonstrate the
// example of < operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;

    cout << "(x < y) : " << (x < y) << endl;

    if (x < y)
        cout << x << " is less than to " << y << endl;
    else
        cout << x << " is not less than to " << y << endl;

    x = 20;
    y = 30;

    cout << "(x < y) : " << (x < y) << endl;

    if (x < y)
        cout << x << " is less than to " << y << endl;
    else
        cout << x << " is not less than to " << y << endl;

    return 0;
}

Output:

(x < y) : 0
10 is not less than to 10
(x < y) : 1
20 is less than to 30

4) Less Than or Equal To operator (<=)

Less Than or Equal To (<=) operator compares both operands and returns 1 if the first operand is less than or equal to the second operand; 0, otherwise.

Syntax:

operand1 <= operand2

Example:

// C++ program to demonstrate the
// example of <= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;

    cout << "(x <= y) : " << (x <= y) << endl;

    if (x <= y)
        cout << x << " is less than or equal to " << y << endl;
    else
        cout << x << " is not less than or equal to " << y << endl;

    x = 40;
    y = 30;

    cout << "(x <= y) : " << (x <= y) << endl;

    if (x <= y)
        cout << x << " is less than or equal to " << y << endl;
    else
        cout << x << " is not less than or equal to " << y << endl;

    return 0;
}

Output:

(x <= y) : 1
10 is less than or equal to 10
(x <= y) : 0
40 is not less than or equal to 30

5) Greater Than operator (>)

Greater Than operator (>) operator compares both operands and returns 1 if the first operand is greater than the second operand; 0, otherwise.

Syntax:

operand1 > operand2

Example:

// C++ program to demonstrate the
// example of > operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;

    cout << "(x > y) : " << (x > y) << endl;

    if (x > y)
        cout << x << " is greater than " << y << endl;
    else
        cout << x << " is not greater than " << y << endl;

    x = 40;
    y = 30;

    cout << "(x > y) : " << (x > y) << endl;

    if (x > y)
        cout << x << " is greater than " << y << endl;
    else
        cout << x << " is not greater than " << y << endl;

    return 0;
}

Output:

(x > y) : 0
10 is not greater than 10
(x > y) : 1
40 is greater than 30

6) Greater Than or Equal To operator (>=)

Greater Than or Equal To operator (>=) operator compares both operands and returns 1 if the first operand is greater than or equal to the second operand; 0, otherwise.

Syntax:

operand1 >= operand2

Example:

// C++ program to demonstrate the
// example of >= operator

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;

    cout << "(x >= y) : " << (x >= y) << endl;

    if (x >= y)
        cout << x << " is greater than or equal to " << y << endl;
    else
        cout << x << " is not greater than or equal to " << y << endl;

    x = 20;
    y = 30;

    cout << "(x >= y) : " << (x >= y) << endl;

    if (x >= y)
        cout << x << " is greater than or equal to " << y << endl;
    else
        cout << x << " is not greater than or equal to " << y << endl;

    return 0;
}

Output:

(x >= y) : 1
10 is greater than or equal to 10
(x >= y) : 0
20 is not greater than or equal to 30

C++ program to demonstrate the example of various relation operators

// C++ program to demonstrate the example
// of various relation operators

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 20;

    // comparing
    cout << "(x == y) : " << (x == y) << endl;
    cout << "(x != y) : " << (x != y) << endl;
    cout << "(x < y)  : " << (x < y) << endl;
    cout << "(x <= y) : " << (x <= y) << endl;
    cout << "(x > y)  : " << (x > y) << endl;
    cout << "(x >= y) : " << (x >= y) << endl;

    // comparing using conditions
    if (x == y)
        cout << x << " is equal to " << y << endl;
    else
        cout << x << " is not equal to " << y << endl;

    if (x != y)
        cout << x << " is not equal to " << y << endl;
    else
        cout << x << " is equal to " << y << endl;

    if (x < y)
        cout << x << " is less than to " << y << endl;
    else
        cout << x << " is not less than to " << y << endl;

    if (x <= y)
        cout << x << " is less than or equal to " << y << endl;
    else
        cout << x << " is not less than or equal to " << y << endl;

    if (x > y)
        cout << x << " is greater than " << y << endl;
    else
        cout << x << " is not greater than " << y << endl;

    if (x >= y)
        cout << x << " is greater than or equal to " << y << endl;
    else
        cout << x << " is not greater than or equal to " << y << endl;

    return 0;
}

Output:

(x == y) : 0
(x != y) : 1
(x < y)  : 1
(x <= y) : 1
(x > y)  : 0
(x >= y) : 0
10 is not equal to 20
10 is not equal to 20
10 is less than to 20
10 is less than or equal to 20
10 is not greater than 20
10 is not greater than or equal to 20

Related Articles
























































0 comments
Leave a comment