In C++, relational operators are used to compare values or expressions. These check the relationship between the operands and return a result in a boolean (true or false).
These comparisons are based on conditions like equality, inequality, greater than, less than, etc.
Relational operators are a fundamental part of a programming language as they help in decision-making, loops, and conditional checks.
List of Relational OperatorsThis is the following list of the relational operators in C++ −
Relational operators are used to compare two values or objects in C++. Here we will see the list of following, where relational operators can be used.
The following is an example of relational operators:
#include <iostream> #include <cmath> // For floating-point comparison #include <string> using namespace std; // Custom class to overload relational operators class Point { public: int x, y; Point(int x, int y) : x(x), y(y) {} bool operator>(const Point& p) { return (x*x + y*y) > (p.x*p.x + p.y*p.y); } bool operator==(const Point& p) { return (x == p.x && y == p.y); } }; int main() { // Comparing Integers int a = 5, b = 10; cout << (a > b) << " " << (a == b) << endl; // Comparing Floating-Point Numbers double x = 5.0, y = 5.0000001; cout << (fabs(x - y) < 1e-6) << endl; // Comparing Characters char c1 = 'A', c2 = 'B'; cout << (c1 < c2) << " " << (c1 == c2) << endl; // Comparing Strings string str1 = "apple", str2 = "banana"; cout << (str1 < str2) << " " << (str1 == str2) << endl; // Comparing Objects Point p1(3, 4), p2(5, 12); cout << (p1 > p2) << " " << (p1 == p2) << endl; return 0; }Output
0 0 1 1 0 1 0 0 0Relational Operators and Conditional Statements
In C++, Relational Operators in conditional statements help the program with decision-making and give results (true or false) based on the result of the comparison.
Syntax for if-else with Relational OperatorsHere we will see the syntax for if-else with relational operators.
if (a > b) { // a is greater than b } else if (a == b) { // a is equal to b } else { // a is less than b }Syntax for while Loop with Relational Operators
Here we will see the syntax for while loop with relational operators.
int i = 1; while (i <= 5) { if (i < 5) { // i is less than 5 } else if (i == 5) { // i is equal to 5 } i++; }Relational Operators with Logical Operators
In C++, relational operators (>, <, ==, !=, >=, <=) can be combined with logical operators (&&, ||, !) to form complex expressions, which allow for more advanced decision-making. this is helpful when you need to check multiple conditions in one expression.
Example#include <iostream> using namespace std; int main() { int age = 25, height = 180; // Check if age is between 18 and 30 and height is greater than 170 if (age >= 18 && age <= 30 && height > 170) cout << "The person qualifies!" << endl; // Check if age is outside 18-30 or height is <= 170 if (age < 18 || age > 30 || height <= 170) cout << "The person does not qualify!" << endl; // Check if age is NOT between 18 and 30 or height is NOT > 170 if (!(age >= 18 && age <= 30) || !(height > 170)) cout << "The person does not qualify due to NOT condition!" << endl; return 0; }Output
The person qualifies!Limitations of Relational Operators
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4