The Big Idea:
Comparison operators are fundamental tools in programming used to compare two values. The result of a comparison is always a Boolean value: either true or false. These operators form the basis of decision-making in code, enabling conditional logic, loop control, and algorithm branching.
Understanding and using comparison operators correctly is essential for any program that responds to data, chooses between options, or evaluates conditions—which means almost every program.
What Are Comparison Operators?
Comparison operators evaluate the relationship between two values (numbers, strings, variables, expressions) and return true or false.
Here is a table of the common comparison operators:
| Operator | Description | Example (Python) | Result |
|---|---|---|---|
== | Equal to | 3 == 3 | True |
!= | Not equal to | 3 != 4 | True |
> | Greater than | 5 > 2 | True |
< | Less than | 5 < 2 | False |
>= | Greater than or equal to | 5 >= 5 | True |
<= | Less than or equal to | 4 <= 4 | True |
All these expressions evaluate to Boolean values (True or False in Python, true or false in Java or JavaScript).
Example: Python Code
x = 10
y = 5
print(x > y) # True
print(x == y) # False
print(x != 10) # False
Each comparison checks the relationship between the values of x and y.
String Comparisons
Comparison operators also work on strings using lexicographical order (alphabetical, based on ASCII/Unicode):
print("apple" < "banana") # True
print("cat" == "Cat") # False (case-sensitive)
"apple"comes before"banana"→True"cat"is not the same as"Cat"→False
This is often counterintuitive for beginners. For example, "Zebra" < "apple" returns True, because uppercase letters come before lowercase letters in ASCII.
Comparison in Logic and Control Flow
Comparison operators are typically used with if, while, or ternary statements:
if age >= 18:
print("You can vote.")
else:
print("Too young to vote.")
Here, age >= 18 is a comparison that controls which block of code runs.
Combining Comparisons: Logical Operators
Comparison operators are often combined using logical operators:
| Logical Operator | Meaning | Example |
|---|---|---|
and | Both must be true | x > 5 and x < 10 |
or | At least one is true | x < 0 or x > 100 |
not | Negates the result | not (x == 5) |
This lets you build more complex conditions.
Worked Example – IB Style
Command Term: Construct
Question:
Construct a condition that checks whether a user’s age is between 13 and 18, inclusive.
Correct Answer:
if age >= 13 and age <= 18:
print("You are a teenager.")
Common Error:
Students sometimes write:
if 13 <= age <= 18:
This is valid in Python (and elegant), but not in many other languages like Java or JavaScript. So be cautious about language-specific features.
Edge Cases to Watch Out For
Floating point comparisons can be unreliable due to precision:
0.1 + 0.2 == 0.3 # FalseAlways be careful when comparing floats; consider using tolerances.
- Object comparisons (in languages like Java) may compare references, not content.
Summary
You should know:
- Comparison operators evaluate relationships between values.
- They return Boolean results:
trueorfalse. - They are crucial for control flow (e.g., if-statements, loops).
You should be able to:
- Use all six basic comparison operators.
- Combine comparisons using logical operators.
- Understand how comparisons behave for numbers, strings, and edge cases (like floating point).