Operators: Comparison Operators
Learn about the comparison operators in C++ and how to use them.
We'll cover the following
In this lesson, we’ll learn about the C++ comparison operators.
Comparison operators
We use comparison operators to compare two or more values.
We can see the different comparison operators in the image below.
The table below shows comparison operators with examples where x
and y
can be any value.
One thing to note is that using the comparison operators, the expression either evaluates to
1
if it is true or0
if it is false.
Comparison Operator
Operators | Meaning | Example |
| Less than |
|
| Greater than |
|
| Greater than equals to |
|
| Less than equals to |
|
| Double equals to |
|
| Not equals to |
|
In C++, a nonzero value in any condition is considered true, and a zero is considered as false.
Quiz
Let’s solve the quiz below to check our understanding of the logical, assignment, and comparison operators.
If int x = 10, y = 11, a = 10, b = 30
and c = 0
, what will be the final value of c
, x
, and y
?
c += (x < y);
c += ((x < y) && (x + y <= b));
c += ((x + 20) < b && (++x <= 11))
c = 1
, x = 10
, y = 11
c = 2
, x = 10
, y = 11