Comparison Operators
Learn about the comparison operator used in C++.
We'll cover the following...
As the name implies, conditional statements specify whether a statement or a block of statements should be executed or not based on some condition being met. These are often called selection constructs. The two general types are:
if…else
switch…case
Conditional statements depend on some comparisons or checks. These are done using comparison operators.
Comparison operators
The conditions tested are specified using comparison operators, also called relational operators. These operators cause the immediate statement in which they are contained to return a boolean value of either true
or false
.
<variable_type> <operator> <variable_name>
Note: In certain circumstances they may evaluate to
0
or1
. Be careful combining conditional statements with arithmetic.
The following comparison operators are available:
Equal to (
==
), not equal to (!=
) of any primitive data type (int
,char
,float
,bool
, etc). These are binary operators (take two operands).Greater-than (
>
), greater than or equal to (>=
), less than (<
), and less than or equal to (<=
) are also binary operators.
Logical operators
C++ also allows logical operations. These, too, return the boolean value to the statements they are used in. Three common types of logical operators used in C++ are discussed below.
Negation (
!
) is a unary operator, and prefixes the operand. It’s used to reverse the truth value of an expression.AND (
&&
) is a logical binary operator that returns the boolean valuetrue
only if both operands are not zero or notfalse
.OR (
||
) is a logical binary operator that returns the boolean valuetrue
if both or even one of the operands is not zero or notfalse
.
The order of precedence in logical operators is as follows: !
has the highest precedence, then comes &&
, and finally ||
with the least precedence. Logical operators are also left-associative, so operators with the same precedence are evaluated from left to right.
You can also see how these operators work in C++:
#include <iostream>using namespace std;int main() {int a , b , c;a = 1;b = 1;c = 0;cout << "A = " << a << ", B = " << b << ", C = " << c << endl;// EQUAL TOcout << "A == B: " << (a == b) << endl;cout << "A != C: " << (a != c) << endl;// GREATER THAN, LESS THANcout << "A > C : " << (a > c) << endl;cout << "A <= C: " << (a <= c) << endl;// ANDcout << "A AND B: " << (a && b) << endl;cout << "A AND C: " << (a && c) << endl;// ORcout << "A OR B: " << (a || b) << endl;cout << "A OR C: " << (a || c) << endl;//NOTcout << "NOT C: " << !c << endl;return 0;}
Here are some examples of the results of computing for different comparison operators:
Expression | Result |
|
|
|
|
|
|
|
|
|
|
|
|
| syntax error |
|
|
Comparison of float
and double
Comparing floating-point numbers such as float
and double
is not the same as comparing integers because of how they are stored in memory. Due to their storage in memory, two variables having the same floating-point number may or may not pass the equality test. Floating-point numbers are stored in binary format, and not all real numbers can be represented exactly in binary, causing rounding and, eventually, limited precision. float
has relatively lower precision that double
, meaning double
can store many more decimal places than float
. Take a look at this example of comparing float
and double
:
#include <iostream>#include <iomanip>using namespace std;int main() {double a = 0.3;double b = 0.1 + 0.2;// Comparing floats and doubles for equalitycout << "a is " << a << ", b is " << b << " and result of (a == b) is " << (a==b) << endl;cout << "If we set the precision to 17, the value of a and b becomes:" << endl;cout << setprecision(17);cout << "a = " << a << endl << "b = " << b;return 0;}
You can see that the compiler doesn’t consider both the values as equal because of how they are actually stored in memory.
Note that in the above code, we have used #include <iomanip>
and cout << setprecision(17);
to make sure that cout << "a = " << a << endl << "b =" << b;
statement displays the values of a
and b
to a precision of 17
decimal places. Ignore these for now. This example was there to show you how the floating-point data is actually stored.
Let’s solve a quick activity to test our understanding:
!1
True
2.01 >= 2.01
False
2 || 0
It could be true or false depending on how the data is stored in the memory.
0 && 5
Let’s test your understanding of logical operator’s precedence and associativity:
What is the result of the following statement?
true || false && false
True
False