Skip to the content of the web site.

Lesson 1.9: Comparison operators

Previous lesson Next lesson


If you are interested, the link making decisions compares the fuzzy thinking of humans with the binary (true/false) processes of computers.

In C++, we will need to execute statements based on whether or not a condition is either true or false. You have already done this in your secondary school mathematics courses where you defined, for example,

$|x| = \left \{ \begin{matrix} x & x \ge 0 \\ -x & x < 0 \end{matrix} \right.$

Thus, we must have some means of representing true and false; however, the ${\rm T}$ and ${\rm F}$ symbols are not numbers, and computers can only deal with numbers, and therefore we will instead represent true with 1 and false with 0.

Note that 0 and 1 are used in other situations where there are binary choices. One of these are on/off switches. On a power bar, you have 0 and 1 to denote off and on:

and if a single button toggles between on and off, they use a 1 superimposed over a 0:

To prevent suggestion of cultural bias, these 1s and 0s are referred to as lines and circles, but the Arabic influence is rather obvious.

Now, we have already seen arithmetic operations that return integers (and later we will see that they can also return real numbers, or at least representations of real numbers). We will now look at six more binary operations that return Boolean values: we will call these comparison operators, for they will be used to compare the two operands. They are:

  1. less than <,
  2. greater than <,
  3. less than or equal to <=,
  4. greater than or equal to >=,
  5. equal to ==, and
  6. not equal to !=.

Less than and greater than

Very often we will want to compare two numbers: is one number greater than or less than another number. In C++, we simply use < and >, as is demonstrated here and you can run this program at repl.it.

// Pre-processor include directives
#include <iostream>

// Function declarations
int main();

// Function definitions
int main() {
	std::cout << "The result of 3 < 5: ";
	std::cout << (3 < 5);
	std::cout << std::endl;

	std::cout << "The result of 7 < 5: ";
	std::cout << (7 < 5);
	std::cout << std::endl;

	std::cout << "The result of 3 > 5: ";
	std::cout << (3 > 5);
	std::cout << std::endl;

	std::cout << "The result of 7 > 5: ";
	std::cout << (7 > 5);
	std::cout << std::endl;

	return 0;
}

When you execute this, you will see that the value of these binary comparison operators is either 0 or 1, depending on whether or not the two operands are in fact either less than or greater than each other.

Important concept:
Note that in mathematics, you write $3 < 5$ and $m < n$ to indicate that "yes, the first is indeed less than the second", while you would write $3 \nless 5$ and $m \nless n$ to indicate that "no, the first is not less than the second". In C++, < and > are binary comparison operators that return either true (1) or false (0) depending on whether or not the operands actually satisfy the given relationship.

Less than or equal to and greater than or equal to

At other times, we may want to ask whether or not one number is less than or equal to another number, or if one number is greater than or equal to another number. For example, in a game, you may get a prize if you accumulate 1000 or more tokens, or a pacemaker may shock the heart if interval between heartbeats is greater or equal to a given period of time.

While you can write $3 \le 4$ and $5 \ge 2$ on the blackboard, these symbols do not appear on keyboards, so C++ allows you to create a single operator with two characters: <= and >=. You can copy this code into the body of the above main() function or run it at repl.it:

	std::cout << "The result of 3 <= 5: ";
	std::cout << (3 <= 5);
	std::cout << std::endl;

	std::cout << "The result of 7 <= 7: ";
	std::cout << (7 <= 7);
	std::cout << std::endl;

	std::cout << "The result of 8 <= 1: ";
	std::cout << (8 <= 1);
	std::cout << std::endl;

	std::cout << "The result of 3 >= 5: ";
	std::cout << (3 >= 5);
	std::cout << std::endl;

	std::cout << "The result of 7 >= 7: ";
	std::cout << (7 >= 7);
	std::cout << std::endl;

	std::cout << "The result of 8 >= 1: ";
	std::cout << (8 >= 1);
	std::cout << std::endl;

Note that if you try to use =< or =>, the compiler will not recognize what you are trying to do: you will get a compilation error. Try running the following program:

// Pre-processor include directives
#include <iostream>

// Function declarations
int main();

// Function definitions
int main() {
	std::cout << "The result of 3 =< 4: ";
	std::cout << (3 =< 4);
	std::cout << std::endl;

	retrun 0;
}

If you were to try to run this program on repl.it, you would get the error message:

example.cpp:10:19: error: expected primary-expression before '<' token
        std::cout << (3 =< 4);
                         ^
example.cpp:13:2: error: 'retrun' was not declared in this scope
         retrun 0;
         ^

Remembering <= and >=
Always write these operators exactly how you say them: for $3 \le 42$ you say "3 is less than or equal to 42", so write 3 <= 42; and for $91 \ge 42$ you say "91 is greater than or equal to 42", so write 91 >= 42. You almost never say, for example, "3 is equal to or less than 42".

Equal to

The next relationship we want to describe is equality ==. It is absolutely important to see that to test for equality, you must use two equal signs. You can copy this code into the body of the above main() function or run it on repl.it:

	std::cout << "The result of 3 == 5: ";
	std::cout << (3 == 5);
	std::cout << std::endl;

	std::cout << "The result of 42 == 42: ";
	std::cout << (42 == 42);
	std::cout << std::endl;

	std::cout << "The result of -0 == 0: ";
	std::cout << (-0 == 0);
	std::cout << std::endl;

Again, in mathematics, we normally write $m = n$ meaning that $m$ has the same value as $n$, and if they are different, we write $m \ne n$. In this case, however, == is a binary comparison operator that returns true (1) if the operands are indeed equal, and false (0) otherwise (that is, they are not equal).

Not equal to

The binary operator != returns true (1) if the operands are not equal, and false (0) if they are equal. The previous link to repl.it includes these commands.

	std::cout << "The result of 3 != 5: ";
	std::cout << (3 != 5);
	std::cout << std::endl;

	std::cout << "The result of 42 != 42: ";
	std::cout << (42 != 42);
	std::cout << std::endl;

	std::cout << "The result of -0 != 0: ";
	std::cout << (-0 != 0);
	std::cout << std::endl;

When you see !=, always say "not equal to" or "not equals", where the ! is voiced as "not".

Sequences of binary comparison operations

Suppose you have the following sequence of binary comparison operations:

	std::cout << "The result of 3 < 4 < 5: ";
	std::cout << (3 < 4 < 5);

If you run this, you get the desired answer:

The result of 3 < 4 < 5: 1

This seems to be right, but is it? Try the following:

	std::cout << "The result of 5 > 4 > 3: ";
	std::cout << (5 > 4 > 3);

This time, if you execute this, you get:

The result of 5 > 4 > 3: 0

But is it not true that $5 > 4 > 3$? Remember, however, that this is no longer mathematics, but a programming language. Just like the arithmetic operations, these operations are evaluated one operation at a time:

                       5 > 4 > 3

is interpreted as

                      (5 > 4) > 3

And 5 > 4, is true, so this evaluates to 1. Now it tries to evaluate 1 > 3, but this is false, so it returns 0.

The only reason the previous version appeared to work correctly is that (3 < 4) < 5 evaluated to 1 < 5, which is also true, so it evaluated to 1.

You may recall from binary arithmetic operators, multiplication and division are evaluated before addition and subtraction and each are evaluated from left to right. The only way to change this is to use parentheses.

Similarly, for binary comparison operators, <, <=, > and >= are evaluated left-to-right and all are evaluated before == and !=, which are themselves evaluated from left-to-right, as well.

Therefore, if you saw the expression

      3 < 4 == 5 >= 6 <= 7 != 8

this would be the same as

      ((3 < 4) == ((5 >= 6) <= 7)) != 8

The most deeply nested binary comparison operators are evaluated first, so

      (1 == (0 <= 7)) != 8

Now, we evaluate 0 <= 7, which returns 1; next, we evaluate 1 == 1, which returns 1 as the they are equal, and then we ask 1 != 8, which is true. Thus, the result of this operation is 1.

Here are some more examples of operator precedence on repl.it.

Thus, we come to one of our most important lessons:

When using any binary operators other than arithmetic operations (addition, subtraction, multiplication and division where you can use standard order of operations), always put parentheses around those operators you intend to evaluate first so that the reader has no doubt what you meant.

There are no circumstances where this author may see one requiring to use a sequence of comparision operators. This is more highlighting the need for parentheses to highlight the order you mean, and not relying on precedence. It is also meant to reinforce that statements like 3 < 4 < 5 evaluate to the "correct" answer for the wrong reasons.

Questions and practice:

1. What are the binary comparison operators that return true if $m \le n$, $m > n$, $m \ne n$, $m < n$, $m = n$ and $m \ge n$.

2. If m < n returns true, what operation returns the same truth statement when n is the first operand and m is the second?

3. Repeat Question 2 for m <= n, m > n, m >= n, m == n, and m != n.


Previous lesson Next lesson