01 / 20
\(<\)
\(==\)
\(>\)

Comparison
Operators in C++

Learn how to use comparison operators to form boolean expressions that evaluate to true or false: the foundation of decision-making in every C++ program.

C++

What Is a Boolean Expression?

A boolean expression is any expression that evaluates to one of exactly two values: true or false. In C++, these map to the integer values 1 (true) and 0 (false).

bool result = (5 \(\texttt{>}\) 3); // result is true (1) bool check = (2 \(\texttt{==}\) 7); // check is false (0)

The Six Comparison Operators

\(\texttt{==}\)
Equal to
\(\texttt{!=}\)
Not equal to
\(\texttt{<}\)
Less than
\(\texttt{>}\)
Greater than
\(\texttt{<=}\)
Less than or equal
\(\texttt{>=}\)
Greater than or equal

How a Comparison Evaluates

\(10\)
Left Operand
\(\ge\)
Operator
\(5\)
Right Operand true → ←
\(10 \ge 5\)
Result: true

The compiler evaluates the expression left operand, then operator, then right operand, and produces a single bool result.

Core Rule: Syntax and Return Type

// General form: operand_A comparison_operator operand_B // Always returns a bool (true or false)
1
Both operands must be of compatible types (numeric, char, or pointer).
2
The result is always a bool: true or false.
3
Operands are not modified: comparison operators only read values, never change them.

Operator Reference Table

Operator Meaning Example Result
\(\texttt{==}\)Equal to\(\texttt{5 == 5}\)true
\(\texttt{!=}\)Not equal to\(\texttt{5 != 3}\)true
\(\texttt{<}\)Less than\(\texttt{3 < 7}\)true
\(\texttt{>}\)Greater than\(\texttt{3 > 7}\)false
\(\texttt{<=}\)Less than or equal\(\texttt{4 <= 4}\)true
\(\texttt{>=}\)Greater than or equal\(\texttt{2 >= 9}\)false

\(\texttt{==}\) versus \(\texttt{=}\): The Critical Difference

\(\texttt{==}\) (Comparison)
Tests whether two values are equal. Returns true or false. Does not change either operand.
\(\texttt{=}\) (Assignment)
Assigns the right-hand value to the left-hand variable. Changes the variable's contents.
if (x \(\texttt{==}\) 5) // Correct: checks if x equals 5 if (x \(\texttt{=}\) 5) // Bug! Assigns 5 to x, condition is true (non-zero)

How to Apply Comparison Operators

1
Identify the two values you need to compare (variables, literals, or expressions).
2
Choose the correct operator: do you need equality, inequality, or an ordering test?
3
Write the expression in the form A operator B.
4
Use the result in a control structure (if, while, for) or store it in a bool variable.
5
Double-check: did you use \(\texttt{==}\) (not \(\texttt{=}\)) for equality? Are boundary cases handled with \(\texttt{<=}\) or \(\texttt{>=}\)?

Example 1: Simple if Statement

int age = 17; if (age \(\texttt{>=}\) 18) { cout << "You can vote!"; } else { cout << "Not old enough yet."; }

Example 2: Loop Condition

int count = 1; while (count \(\texttt{<=}\) 5) { cout << count << " "; count++; } // Output: 1 2 3 4 5

Notice: using \(<\) instead of \(\le\) would print only 1 2 3 4, missing the last value. The boundary operator you choose matters.

Example 3: Chaining with Logical Operators

int score = 85; if (score \(\texttt{>=}\) 70 \(\texttt{&&}\) score \(\texttt{<}\) 90) { cout << "Grade: B"; }

Key insight: You cannot write the mathematical expression \( 70 \le \texttt{score} < 90 \) directly in C++. You must explicitly use two separate comparisons connected by \(\texttt{&&}\).

Comparing Characters

In C++, char values are compared by their ASCII codes. Letters later in the alphabet have higher codes. Uppercase and lowercase letters occupy different ranges.

char ch = 'B'; if (ch \(\texttt{>=}\) 'A' \(\texttt{&&}\) ch \(\texttt{<=}\) 'Z') { cout << "Uppercase letter"; } // 'A' = 65, 'B' = 66, ..., 'Z' = 90

Floating-Point Pitfall

double a = 0.1 + 0.2; double b = 0.3; if (a \(\texttt{==}\) b) { cout << "Equal"; // May NOT print! }
// Safer approach: check if difference is tiny if (abs(a - b) \(\texttt{<}\) 1e-9) { cout << "Close enough!"; }

Common Mistakes

✕ Using = instead of ==
Writing \(\texttt{if (x = 5)}\) assigns 5 to x and always evaluates as true. Use \(\texttt{==}\) for comparison.
✕ Chaining like math
Writing \(1 < x < 10\) compiles but doesn't check a range. Use \((x > 1 \ \texttt{&&}\ x < 10)\).
✕ Comparing floats with ==
Floating-point rounding means \(0.1 + 0.2 \ne 0.3\). Compare using an epsilon threshold instead.

Summary at a Glance

Concept Key Point Example
Return type Always bool true / false
Equality Use ==, not = \(\texttt{x == 5}\)
Boundary \(\texttt{<=}\) includes the endpoint \(\texttt{count <= 10}\)
Range check Use && with two operators \(\texttt{x >= 1 \&\& x <= 100}\)
Floats Avoid == on doubles \(\texttt{abs(a - b) < 1e-9}\)
Characters Compared by ASCII code \(\texttt{'a' < 'z'}\) is true

Question 1 of 5

What does the following expression evaluate to?

\(\texttt{7 != 7}\)

Question 2 of 5

Which line correctly checks if the variable temp is greater than or equal to 100?

Question 3 of 5

What is the value of result after this code runs?

int x = 4;
bool result = (x \(\texttt{<}\) 4);

Question 4 of 5

A student writes: \(\texttt{if (1 < x < 10)}\) intending to check that x is between 1 and 10. What actually happens?

Question 5 of 5

Why is it dangerous to use \(\texttt{==}\) to compare two double values in C++?