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
EasyYou are provided with the following variables:
temperature = 22
is_sunny = true
has_umbrella = false
is_weekend = true
has_homework = falseUse the && and || operators to evaluate and display the following expressions, each on a separate line:
- Check if
temperatureis greater than20andis_sunnyistrue - Check if
is_sunnyistrueorhas_umbrellaistrue - Check if
is_weekendistrueandhas_homeworkisfalse - Check if
temperatureis less than10ortemperatureis greater than30 - Check if
is_weekendistrueandis_sunnyistrueandtemperatureis greater than or equal to20
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 >= 20This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False