Menu
Coddy logo textTech

Logical Operators Part 1

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

Comparison operators let you compare two values, but what if you need to check multiple conditions at once? Logical operators allow you to combine boolean expressions to create more complex conditions.

The && operator (called "and") returns true only when both conditions are true. If either condition is false, the entire expression is false:

age = 25
has_license = true

age >= 18 && has_license  # true (both conditions are true)
age >= 18 && false        # false (one condition is false)

The || operator (called "or") returns true when at least one condition is true. It only returns false when both conditions are false:

is_weekend = true
is_holiday = false

is_weekend || is_holiday  # true (at least one is true)
false || false            # false (both are false)

Think of && as requiring all conditions to pass, while || only needs one to pass. These operators are essential for building real-world logic, like checking if a user is both logged in and has permission, or if a store is open on weekends or holidays.

challenge icon

Challenge

Easy

You are provided with the following variables:

temperature = 22
is_sunny = true
has_umbrella = false
is_weekend = true
has_homework = false

Use the && and || operators to evaluate and display the following expressions, each on a separate line:

  1. Check if temperature is greater than 20 and is_sunny is true
  2. Check if is_sunny is true or has_umbrella is true
  3. Check if is_weekend is true and has_homework is false
  4. Check if temperature is less than 10 or temperature is greater than 30
  5. Check if is_weekend is true and is_sunny is true and temperature is greater than or equal to 20

Each output should be either true or false.

Cheat sheet

Logical operators allow you to combine boolean expressions to create more complex conditions.

The && operator (called "and") returns true only when both conditions are true:

age = 25
has_license = true

age >= 18 && has_license  # true (both conditions are true)
age >= 18 && false        # false (one condition is false)

The || operator (called "or") returns true when at least one condition is true:

is_weekend = true
is_holiday = false

is_weekend || is_holiday  # true (at least one is true)
false || false            # false (both are false)

You can chain multiple logical operators together:

condition1 && condition2 && condition3  # all must be true
condition1 || condition2 || condition3  # at least one must be true

Try it yourself

# Variables are already defined for you
temperature = 22
is_sunny = true
has_umbrella = false
is_weekend = true
has_homework = false

# TODO: Write your code below
# Use && and || operators to evaluate the expressions
# Print each result on a separate line

# 1. Check if temperature > 20 AND is_sunny is true


# 2. Check if is_sunny is true OR has_umbrella is true


# 3. Check if is_weekend is true AND has_homework is false


# 4. Check if temperature < 10 OR temperature > 30


# 5. Check if is_weekend is true AND is_sunny is true AND temperature >= 20
quiz iconTest yourself

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

All lessons in Fundamentals