Menu
Coddy logo textTech

Comparison Operators

Part of the Fundamentals section of Coddy's Ruby journey — lesson 15 of 88.

While arithmetic operators perform calculations, comparison operators let you compare values. These operators always return a boolean result: either true or false.

Ruby provides six comparison operators:

==Equal to
!=Not equal to
Greater than
<code><Less than
>=Greater than or equal to
<=Less than or equal to

Here's how they work in practice:

10 == 10   # true
5 != 3     # true
7 > 4      # true
2 < 1      # false
5 >= 5     # true
8 <= 6     # false

Notice that equality uses double equals (==), not a single equals sign. A single = is for assignment, while == is for comparison. This is a common source of bugs for beginners, so keep it in mind!

Comparison operators become essential when you need your program to make decisions based on conditions, which you'll explore in upcoming lessons.

challenge icon

Challenge

Easy

You are provided with the following variables:

player_score = 85
high_score = 100
passing_score = 70
player_lives = 3
max_lives = 3

Use comparison operators to evaluate and display the following comparisons, each on a separate line:

  1. Check if player_score is equal to high_score
  2. Check if player_score is not equal to passing_score
  3. Check if player_score is greater than passing_score
  4. Check if player_score is less than high_score
  5. Check if player_lives is greater than or equal to max_lives
  6. Check if player_score is less than or equal to high_score

Each output should be either true or false.

Cheat sheet

Comparison operators compare values and return a boolean result (true or false).

Ruby provides six comparison operators:

==Equal to
!=Not equal to
Greater than
<code><Less than
>=Greater than or equal to
<=Less than or equal to

Examples:

10 == 10   # true
5 != 3     # true
7 > 4      # true
2 < 1      # false
5 >= 5     # true
8 <= 6     # false

Note: Use == for comparison, not = (which is for assignment).

Try it yourself

# Game score variables
player_score = 85
high_score = 100
passing_score = 70
player_lives = 3
max_lives = 3

# TODO: Write your code below
# Use comparison operators to evaluate each comparison and print the result

# 1. Check if player_score is equal to high_score

# 2. Check if player_score is not equal to passing_score

# 3. Check if player_score is greater than passing_score

# 4. Check if player_score is less than high_score

# 5. Check if player_lives is greater than or equal to max_lives

# 6. Check if player_score is less than or equal to high_score
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals