Relational Operators in C++
Relational operators in the C++ programming language are used to compare two values, either user-defined or declared variables, or expressions. Relational operators return a Boolean value (either true or false) as output. This completely depends on whether the user-defined comparison condition in the program is met or not. Comparison values in relational operators are mostly used in user-defined conditional statement logic expressions like if, while, and for loops to control and manage the default flow of a program based on some specific condition.

Detail of Relational Operators
| Operator symbol | Operator Name | Operator Description | Example |
| == | Equal to | Here it Returns output true if the both operands are same or equal variable value or identical. | p == q |
| != | Not equal to | Here it Returns output true if the operands are not equal or different from each other variable value. | p != q |
| > | Greater than | Here it Returns output true if the left operand variable value is greater than the right value. | p > q |
| < | Less than | Here it Returns output true if the left operand is less than to the right operand variable value. | p < q |
| >= | Greater than or equal to | Here it Returns output true if the left operand is greater than or equal to the right operand value. | p >= q |
| <= | Less than or equal to | Here it Returns output true if the left operand is less than or equal to the right operand value. | p <= q |
So, let’s get to know relational or comparison operators in the C++ programming language better.
Equals (==) Relational Operator in C++.
The Equals (==) operator in a C++ program check and analyses whether two user-defined values in the current program are equal or identical.
Example of the Equals (==) relational operator.
#include <iostream>
using namespace std;
int main() {
int p = 1, q = 1;
if (p == q) {
cout << “Here, the p and q variables are both equal.” << endl;
}
return 0;
}
Explanation of the Equals (==) relational operator.
- Here, the p == q condition in the Equals program checks whether the values p and q are equal or not. Here, both values are defined as 1. Therefore, the result will be true.
Not Equal (!=) Relational Operator in C++.
In any C++ program, the != Not Equal operator checks or analyzes whether two values of a user-defined or declared variable are equal or not.
Example of the Not Equal (!=) relational operator.
#include <iostream>
using namespace std;
int main() {
int p = 1, q = 2;
if (p != q) {
cout << “Here, the p and q variable values are not equal.” << endl;
}
return 0;
}
Not Equal (!=) relational operator explanation.
- Here, in the Not Equal program condition, the p != q expression checks whether the p variable is equal to the q variable. This is because the p variable value is 1 and the q parameter value is 2. Therefore, it will output true.
Greater Than (>) Relational Operator in C++.
In any C++ program, the > Greater Than operator checks and analyzes whether the left operand value is greater than the right operand value for a given user-defined variable value.
Example of the Greater Than (>) Relational Operator.
#include <iostream>
using namespace std;
int main() {
int p = 2, q = 1;
if (p > q) {
cout << “Here, the p value is greater than the q variable value.” << endl;
}
return 0;
}
Greater Than (>) relational operator explanation.
- Here, the p > q expression in the Greater Than program condition checks whether the declared variable p value (2) is greater than the q variable value (1). If this is true, the condition will result in a true output.
Less Than (<) Relational Operator in C++.
In any C++ program, the < less than relational operator checks and analyzes whether the user-defined left operand value is less than the right operand value.
Example of the Less Than (<) Relational Operator.
#include <iostream>
using namespace std;
int main() {
int p = 1, q = 2;
if (p < q) {
cout << “Here, the p value is less than the q variable value.” << endl;
}
return 0;
}
Less Than (<) relational operator explanation.
- Here, the p < q expression in the less-than program condition checks whether p (1) is less than q (2), or not. It will output true.
Greater Than or Equal to (>=) Relational Operator in C++.
In any C++ program, the >= greater than equal to, and to operator checks whether the left user-defined operand value is greater than, or equal to, the right operand value.
Example of the Greater Than or Equal to (>=) Relational Operator.
#include <iostream>
using namespace std;
int main() {
int p = 1, q = 1;
if (p >= q) {
cout << “Here, the p value is greater than or equal to the q variable value.” << endl;
}
return 0;
}
Greater Than or Equal to (>=) relational operator explanation.
- Here, in the greater than and equal to program condition, the p >= q expression checks whether p (1) is greater than and equal to q (1). Since they are equal, the output will be true.
Less Than or Equal to (<=) Relational Operator in C++.
In any C++ program, the <= less than and equal to operator checks whether the left operand value of a user-defined program expression is less than and equal to the right operand value.
Example of the Less Than or Equal to (<=) Relational Operator.
#include <iostream>
using namespace std;
int main() {
int p = 1, q = 2;
if (p <= q) {
cout << “here p variable value is less than or equal to q variable value.” <<endl;
}
return 0;
}
Less Than or Equal to (<=) relational operator explanation.
- Here, in the Less Than and Equal to program condition, the p <= q expression checks whether p (1) is less than and equal to q (2), or not. The result of this expression will be true.
Relational operators with different data types in C++ programming.
Relational operators in C++ programs can be used with multiple data type variable values, such as integers, floating-point numbers, and even string data types. As such, there are a few things to keep in mind before using them.
Floating-point numbers, such as the float or double data types, in C++ programs can have precision output value issues. Especially when comparing numeric values that are very close to each other.
In the string data type, relational operators can be used with two different strings. These user-defined string values are compared in lexicographical order, as per the specified string alphabetical order.
Example of comparing floating-point numbers.
#include <iostream>
using namespace std;
int main() {
double p = 9.0, q = 9.0;
if (p == q) {
cout << “here the p and q variable values are equal.” << endl;
}
return 0;
}
comparing floating-point relational operator explanation.
Here, when comparing floating-point numbers in this program, some tolerance (epsilon) is allowed for floating-point output precision issues.
Example of comparing two text strings.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text1 = “c++”, text2 = “javascript”;
if (text1 != text2) {
cout << “here text1 string and text2 string are not equal.” << endl;
}
return 0;
}
comparing two text strings relational operator explanation.
- This two string comparison != operator compares strings in lexicographical order, i.e., in dictionary sequence order.
Detail explanation of Relational Operators
| Operator symbol | Relational Operators Description | Example |
| == | Equal to operator – here it displays true result if the both users define operands value are same or equal. | p == q |
| != | Not equal to operator – here it displays true if the both users define declare variable value operands are not equal. | p != q |
| > | Greater than operator – here it displays true output result if the left user define operand value is greater than to the right operand value. | p > q |
| < | Less than operator – here it displays true output result if the left operand value is less than the right operand value. | p < q |
| >= | Greater than or equal to operator – here it displays true output result if the left operand is greater than or equal to the right operand value. | p >= q |
| <= | Less than or equal to operator – here it displays true output result if the left operand is less than or equal to the right operand value. | p <= q |
