C++ Relational Operators

In this lesson, you will learn about Relational Operators in C++, and their usages, along with examples to better understand the topic.


What are Relational Operators?

To examine the connection between two operands, employ a relational operator. For instance,

var1 < var2;

The above expression determines whether var1 is greater than var2 using the relational operator <, returning 1 in the true relationship and 0 in the case of a false relationship.

OperatorMeaningExample
!ERROR! A2 -> Formula Error: Unexpected operator '='Is Equal To100 == 200 returns false
!=Not Equal To100 != 200 returns true
>Greater Than100 > 200 returns false
<Less Than100 < 200 returns true
>=Greater Than or Equal To100 >= 200 returns false
<=Less Than or Equal To100 <= 200 returns true

Example of Relational Operators in C++

#include <iostream>

using namespace std;
int main() {
  int var1 = 200, var2 = 100;
  bool output;
  output = (var1 == var2); // false
  cout << "200 == 100 is " << output << endl;
  output = (var1 != var2); // true
  cout << "200 != 100 is " << output << endl;
  output = var1 > var2; // false
  cout << "200 > 100 is " << output << endl;
  output = var1 < var2; // true
  cout << "200 < 100 is " << output << endl;
  output = var1 >= var2; // false
  cout << "200 >= 100 is " << output << endl;
  output = var1 <= var2; // true
  cout << "200 <= 100 is " << output << endl;
  return 0;
}

Output

200 == 100 is 0
200 != 100 is 1
200 > 100 is 1
200 < 100 is 0
200 >= 100 is 1
200 <= 100 is 0

This concludes the C++ Relational Operators lesson. In the next lesson, you will learn about Logical Operators in C++.