Logical Operators (AND, OR)
Part of the Fundamentals section of Coddy's R journey — lesson 17 of 78.
Comparison operators let you check a single condition, but what if you need to check multiple conditions at once? Logical operators allow you to combine conditions and create more complex expressions.
The AND operator (&&) returns TRUE only when both conditions are true:
age <- 25
has_license <- TRUE
can_drive <- age >= 18 && has_license # TRUEIf either condition is false, the entire expression becomes false. Think of it as requiring all conditions to pass.
The OR operator (||) returns TRUE when at least one condition is true:
is_weekend <- TRUE
is_holiday <- FALSE
day_off <- is_weekend || is_holiday # TRUEThe OR operator only returns false when both conditions are false. Here's a quick reference:
| A | B | A && B | A || B |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE | TRUE |
| FALSE | TRUE | FALSE | TRUE |
| FALSE | FALSE | FALSE | FALSE |
You can combine multiple conditions in a single expression using these operators together, building powerful logical checks for your programs.
Challenge
EasyDetermine eligibility for different activities using the AND (&&) and OR (||) operators.
You are provided with the following variables:
age <- 17
has_permission <- TRUE
is_member <- FALSE
has_ticket <- TRUEUse logical operators to create the following variables:
- Create a variable called
can_enter_clubthat storesTRUEif the person is 18 or older AND is a member - Create a variable called
can_watch_moviethat storesTRUEif the person is 18 or older OR has permission - Create a variable called
can_attend_eventthat storesTRUEif the person has a ticket AND is a member - Create a variable called
gets_discountthat storesTRUEif the person is a member OR is under 18 (age less than 18) - Create a variable called
vip_accessthat storesTRUEif the person is 18 or older AND is a member AND has a ticket
Use the print() function to display can_enter_club, can_watch_movie, can_attend_event, gets_discount, and vip_access in that exact order.
Cheat sheet
Logical operators allow you to combine multiple conditions in R.
The AND operator (&&) returns TRUE only when both conditions are true:
age <- 25
has_license <- TRUE
can_drive <- age >= 18 && has_license # TRUEThe OR operator (||) returns TRUE when at least one condition is true:
is_weekend <- TRUE
is_holiday <- FALSE
day_off <- is_weekend || is_holiday # TRUETruth table for logical operators:
| A | B | A && B | A || B |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE | TRUE |
| FALSE | TRUE | FALSE | TRUE |
| FALSE | FALSE | FALSE | FALSE |
You can combine multiple conditions in a single expression to create complex logical checks.
Try it yourself
# Given variables
age <- 17
has_permission <- TRUE
is_member <- FALSE
has_ticket <- TRUE
# TODO: Write your code below
# Create the following variables using logical operators (&&, ||):
# 1. can_enter_club - TRUE if age >= 18 AND is_member
# 2. can_watch_movie - TRUE if age >= 18 OR has_permission
# 3. can_attend_event - TRUE if has_ticket AND is_member
# 4. gets_discount - TRUE if is_member OR age < 18
# 5. vip_access - TRUE if age >= 18 AND is_member AND has_ticket
# Print the results in order
print(can_enter_club)
print(can_watch_movie)
print(can_attend_event)
print(gets_discount)
print(vip_access)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values