Comparison Operators

This article is not assessed by the IB but may be helpful to deepen your understanding. Plus, I think it's cool.

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:

OperatorDescriptionExample (Python)Result
==Equal to3 == 3True
!=Not equal to3 != 4True
>Greater than5 > 2True
<Less than5 < 2False
>=Greater than or equal to5 >= 5True
<=Less than or equal to4 <= 4True

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 OperatorMeaningExample
andBoth must be truex > 5 and x < 10
orAt least one is truex < 0 or x > 100
notNegates the resultnot (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  # False
    

    Always 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: true or false.
  • 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).