C++ Relational Operators

C++ Relational Operators

Relational operators in C++ are the operators that are used to compare two given values. These operators also help in making decisions and controlling the flow of programs in C++. In this article, we will discuss what relational operators are, types of relational operators, relational operators with conditional statements and logical operators, uses of relational operators, and limitations of relational operators in C++.

Table of Contents:

What are Relational Operators in C++?

Relational Operators in C++ are the symbols used to compare two values. These operators return true (1) or false (0). If the condition is met, true (1) is returned, otherwise, false (0) is returned. Relational operators try to find out the relation between the two operands. These operators are also known as comparison operators.

Types of Relational Operators in C++

There are six types of relational operators in C++:

Operator NameSymbolDescriptionSyntax
Equal to==It checks if two values are equal. a == b
Not equal to!=It checks if two values are not equal. a != b
Greater than>It checks if the left value is greater than the right value. a > b
Less than<It checks if the left value is less than the right value. a < b
Greater than or equal>=It checks if the left value is greater than or equal to the right value. a >= b
Less than or equal<=It checks if the left value is less than or equal to the right value. a <= b


Let’s discuss each of these relational operators in brief, with the examples in C.

1. Equal to (==) Operator

The equal to (==) operator in C++ checks if two values are equal, and if they are equal, it will give true (1), otherwise, it will give false (0). It is commonly used in conditional statements, such as if statements.

Syntax:

value1 == value2
Cpp

Output:

Equal to (==) Operator

The code shows that the equal to operator checks if the integers a, b, and c with the values 10, 10, and 5 are equal to each other and then prints the output to the console.

2. Not Equal to (!=) Operator

The not equal to (!=) operator in C++ checks if two values are not equal and gives true (1) if the values are not equal, otherwise, it gives that the values are equal.

Syntax:

value1 != value2

Example:

Cpp

Output:

Not Equal to (!=) Operator

The code shows how the not equal to operator checks that the given integers a and b are not equal to each other. At first, it gives output true (1) because both a and b have different values, but after changing the value of b to 10, it gives output false (0).

3. Less Than (<) Operator

The less than (<) operator in C++ checks if the value on the left side is less than the value on the right side and then returns true (1) if the given condition is true, otherwise, it will return false (0).

Syntax:

value1 < value2

Example:

Cpp

Output:

Less Than Operator

The code shows how the less-than operator checks that the left value is less than the right value. At first, it checks x = 5 and y = 10, and then it checks after changing the value of x to 15 and prints the outputs to the console.

4. Greater Than (>) Operator

The greater than (>) operator in C++ checks if the left value is greater than the right value and returns true (1) if the condition is true; otherwise, it will return false (0).

Syntax:

value1 > value2

Example:

Cpp

Output:

Greater Than ( data-lazy-src=

The code shows how the greater-than operator checks if the left value is greater than the right value. At first, it checks a = 25 and y = 20, and then it checks after changing the value of b to 15 and prints the outputs to the console. 

5. Less Than or Equal To (<=) Operator

The less than or equal to (<=) operator in C++ checks if the left value is less than or equal to the right value, and it returns true (1) if the condition is true; otherwise, it will return false (0).

Syntax:

value1 <= value2

Example:

Cpp

Output:

Less Than or Equal To Operator

The code shows how the less than or equal to operator compares two integers, x and y, and then prints the output by checking with the different values of x.

6. Greater Than or Equal To (>=) Operator

The greater than or equal to operator (>=) in C++ checks if the left value is greater than or equal to the right value and returns true (1) if the condition is true; otherwise, it returns false (0).

Syntax:

value1 >= value2

Example:

Cpp

Output:

Greater Than or Equal To ( data-lazy-src=

The code shows how the greater than or equal to operator compares two integers, a and b, and then prints the output by checking with the different values of a.

Relational Operators with Conditional Statements in C++

The relational operators are used with the conditional statements to control the flow of C++ programs by comparison.

Syntax of if-else with Relational Operators:

if (condition using relational operator) {

    // code runs if condition is true

}

else {

    // code runs if condition is false

}

Example:

Cpp

Output:

Relational Operators with Conditional Statements in C++

The code shows how the relational operators are used within the if-else statements to give grades to the students based on the marks they scored.

Relational Operators with Logical Operators in C++

The relational operators compare each value, and the logical operators combine or invert all the results of multiple expressions to form complex conditions.

Example:

Cpp

Output:

Relational Operators with Logical Operators in C++

The code shows the relational operators checks age and marks, and the logical AND operator ensures that the conditions are true before granting eligibility. 

Relational Operators Usage in C++

  1. Relational operators are used to compare integer data types to check conditions, such as equality, greater than, and less than.
  2. Relational operators can compare float and double values. However, due to precision loss, direct equality comparisons (==) may produce unexpected results.
  3. They compare characters based on ASCII values.
  4. Relational operators can compare memory addresses stored in pointers.
  5. In user-defined types, relational operators can be overloaded to compare objects based on custom logic.
  6. Relational operators are used with the conditional statements to control the flow of program execution.

Limitations of Relational Operators in C++

  • Relational operators cannot handle complex logic without combining them with logical operators.
  • Comparing floating-point values with the relational operators may lead to incorrect results due to precision errors.
  • These operators do not work with user-defined data types until the operators are overloaded.
  • Comparing incompatible data types using relational operators will give compilation errors.
  • Relational operators do not check value ranges directly, so compound conditions must be written manually.

Conclusion

In C++, relational operators are used to compare two values and affect the program flow based on some conditions. Relational operators also assist you in making decisions to compare different data types like integer, character, and user-defined types. Therefore, if you understand its types, its limitations, its uses, and the way relational operators interact with logical operators, you can conveniently apply relational operators to compare the values within C++ programming.

Relational Operators in C++ – FAQs

Q1. What are relational operators in C++?

Relational operators are the operators that are used to compare two values.

Q2. Can I use relational operators with string?

Yes, you can use relational operators with strings because they compare strings in an alphabetical manner.

Q3. Is == reliable for float comparison?

No, it is not reliable because it can lead to precision issues.

Q4. Can I compare custom objects?

Yes, you can compare custom objects in your program.

Q5. What is the difference between == and =?

The difference between == and = is that == checks equality, and = assigns values.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner