Be Careful Comparing Numbers in C++

C++ is a widely used programming language, but also one in which programming errors are most common. Many of these errors are due to logical errors. In particular, errors made during comparison operations can affect the accuracy and reliability of your code.

The C++ language has one particular comparison error that many new developers overlook. This error results from an incorrect understanding of how operators work when performing multiple comparisons. Find out how to avoid this common mistake and why it occurs in the first place.

4

Comparisons in C++

The C++ programming language offers many different features and tools, along with basic operations such as comparison operators. Comparison operators are special operations in programming languages that you’re able to use to compare data with each other. Developers use these operators quite often, especially when creating algorithms.

You can see comparison operators in many different examples in daily life. For example, when you shop at the grocery store, you use these operators in mind to compare prices. If the price of one product is lower than the other, you choose that product.

BBC iPlayer showing on a smart TV.

You can see comparison operators inif-else statementsquite often. Comparison operators are the place to go to check if one value is greater than, less than, or equal to another value. There is a very small but important detail that you should not overlook. Results of comparison expressions return true or false, which are boolean values. These values are one of the basic components of the control structure in programming.

For example, in the C++ programming language, the “==” operator checks if two values are equal. If the values are equal, the result returns true. Otherwise, the result will be false.

Feed view open on a Notion database

An Example of a Comparison Problem

One of the common mistakes C++ beginners make is the use of comparison operators. These operators allow programmers to compare two values and perform different operations based on the result of that comparison. However, using these operators incorrectly can cause unexpected errors.

For example, although the expression 3 < 15 < 10 is mathematically incorrect, C++ considers its result to be true. it’s possible to demonstrate this by writing the following simple test program.

The famous

First, create a file namedtest.cpp. Open this file using yourfavorite code editorand add the following code to it.

You can use this command to compile and run the code:

Comparison operators and boolean logic with cpp

You now have a program calledTest. Run the program and examine its output.

C++ considered 3 < 15 < 10 to be true when running this program. Why might the result come out this way, even though it is a mathematically incorrect statement?

Causes of the Comparison Problem in C++

Like most programming languages, C++ reads code from left to right. Each comparison operator produces a boolean value. Boolean values don’t just mean true and false; they have a mathematical equivalent.

The working principle of acomputer depends on ones and zeros. To a computer, the result of something is either true or false. Computer programs usually treat the number 1 as true and the number 0 as false.

Examine the comparison problem again and read the statement from left to right; you will see that there are two different comparisons. The first comparison is between the numbers 3 and 15. This is a true value because 3 is less than 15.

The second comparison is between that result and the number 10. Since it needs to carry out a numeric comparison, C++ silently converts the boolean true value to 1. 1 is less than 10, so the overall result is true.

In conclusion, although it seems like a mathematical fallacy, this statement is true for C++ and computers.

How to Solve Comparison Problems in C++

C++, along with most other programming languages, uses a different syntax for logical comparison than traditional mathematics. The mathematical expression 3 < a < 15 means “3 is less than aanda is less than 15.” However, as you’ve seen, C++ interprets that expression differently.

To represent and in C++, use the && operator. You can then chain boolean expressions together and build logic using operators like&&to represent AND,||to represent OR, and!to represent NOT. Languages likeJava use the same logical operators.

Using the correct logical operator, you may fix the bug in the earlier example:

Now this code will test if the value a is greater than 3 and whether the value a is less than 10. Compile and run the program and observe the result.

The previous example printed “foo,” but the program now prints “boo,” as intended. The boolean value of the left side of the comparison (3 < a) is true. The value of the right side (a < 10) is false. Sincetrue and falseis alwaysfalse, the overall expression evaluates to false, so that condition fails and theelseblock runs.

Try switching the AND(&&) operator to an OR (||) and observing the different result.

The Importance of Logical Checks in C++

Logical comparisons in C++ involve the use of boolean values and comparison operators. Make sure you use the correct boolean values and comparison operators to control the operation of your programs. It can be difficult to spot badly-formed expressions because C++ will often carry out different behavior rather than failing completely.

Now you know how compilers ignore this issue and treat each comparison as a boolean when reading from left to right. Watch out for this problem in any language you use, and learn to recognize its effects so you could stay one step ahead.

C++ is a fast language, but a complicated one too. So should you really bother to learn it?

Don’t let someone else take over your phone number.

Every squeak is your PC’s way of crying for help.

These films will leave you questioning humanity, but also wanting more.

The best features aren’t the ones being advertised.

The fix was buried in one tiny toggle.

Technology Explained

PC & Mobile