Skip to main content

Relational Operators in C++

Relational operators in C++ are used to compare two values and determine the relationship between them. These operators return a boolean value of true or false based on whether the comparison is true or false. Here are the commonly used relational operators:

Equal to (==)

Compares if two operands are equal. Returns true if they are equal, false otherwise.

int a = 5, b = 5;
if (a == b) {
// This block will be executed because a is equal to b
}

Not equal to (!=)

Compares if two operands are not equal. Returns true if they are not equal, false otherwise.

int a = 5, b = 10;
if (a != b) {
// This block will be executed because a is not equal to b
}

Greater than (>)

Checks if the left operand is greater than the right operand. Returns true if it is, false otherwise.

int a = 10, b = 5;
if (a > b) {
// This block will be executed because a is greater than b
}

Less than ( < )

Checks if the left operand is less than the right operand. Returns true if it is, false otherwise.

int a = 5, b = 10;
if (a < b) {
// This block will be executed because a is less than b
}

Greater than or equal to (>=)

Checks if the left operand is greater than or equal to the right operand. Returns true if it is, false otherwise.

int a = 10, b = 5;
if (a >= b) {
// This block will be executed because a is greater than or equal to b
}

Less than or equal to (< =)

Checks if the left operand is less than or equal to the right operand. Returns true if it is, false otherwise.

int a = 5, b = 10;
if (a <= b) {
// This block will be executed because a is less than or equal to b
}