Home | Computer | Relational Operators in C++ | Relational Expressions

Relational Operators in C++ | Relational Expressions

August 23, 2022

The relational operators are the type of operators used to specify conditions in programs. In a relational operator, two values are compared. Results are presented as true or false.

When the output is true the result will be 1. If the output will be false the result will be shown 0. The relational operators are sometimes called comparison operators as they test conditions that are either true or false.

OperatorDescription
Less than
Greater than
<=Less than or equal to
>=Greater than or equal to
!=Not equal to
==Equal to

List of Relational Operators

The C++ language provides the following six basic relation operators:

Greater than operator (>)

Greater than operator returns true if the value on the left side of > is greater than the value on the right side. Otherwise returns the false.

Example

int main( )

{

int x = 10;

int y = 5;

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

return 0;

}

The output will be 1.

Less than Operator (<)

Less than operator returns true if the value on the left side of < is less than the value on the right side. Otherwise returns the false.

Example

int main( )

{

int x = 10;

int y = 5;

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

return 0;

}

The output will be 0.

Equal To Operator (==)

Equal to operator returns true if the values on both sides of = are equal. Otherwise returns the false.

Example

int main( )

{

int x = 10;

int y = 5;

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

return 0;

}

The output will be 0.

Greater than or equal to the operator (>=)

Greater than or equal to operator returns true if the value on the left side of >= is greater than or equal to the value on the right side. Otherwise retunes the false.

Example

int main( )

{

int x = 10;

int y = 5;

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

return 0;

}

The output will be 1.

Less than or equal to the operator (<=)

Less than or equal to operator returns true if the value on the left side of >= is less than or equal to the value on the right side. Otherwise retunes the false.

Example

int main( )

{

int x = 10;

int y = 5;

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

return 0;

}

The output will be 1.

Not equal to the operator (!=)

The not equal to operator returns true if the values on both sides of = are not equal. Otherwise returns the false.

Example

int main( )

{

int x = 10;

int y = 5;

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

return 0;

}

The output will be 0.

Relational Expression

A type of expression that consists of constants, variables, and relational operators is called relational expression. These are used to compare the values. The result of a relational expression can be true or false.

Example

If A = 8 and B = 6 The relational expressions will be:

Relational ExpressionResult
A > BTrue
A < BFalse
A <= BFalse
A >= BTrue
A == BFalse
A != BTrue