In this lesson, you will learn about Relational Operators in C++, and their usages, along with examples to better understand the topic.
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.
Operator | Meaning | Example |
---|---|---|
!ERROR! A2 -> Formula Error: Unexpected operator ‘=’ | Is Equal To | 100 == 200 returns false |
!= | Not Equal To | 100 != 200 returns true |
> | Greater Than | 100 > 200 returns false |
< | Less Than | 100 < 200 returns true |
>= | Greater Than or Equal To | 100 >= 200 returns false |
<= | Less Than or Equal To | 100 <= 200 returns true |
#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++.