Menu
Coddy logo textTech

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  # TRUE

If 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  # TRUE

The OR operator only returns false when both conditions are false. Here's a quick reference:

ABA && BA || B
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE

You can combine multiple conditions in a single expression using these operators together, building powerful logical checks for your programs.

challenge icon

Challenge

Easy

Determine 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 <- TRUE

Use logical operators to create the following variables:

  1. Create a variable called can_enter_club that stores TRUE if the person is 18 or older AND is a member
  2. Create a variable called can_watch_movie that stores TRUE if the person is 18 or older OR has permission
  3. Create a variable called can_attend_event that stores TRUE if the person has a ticket AND is a member
  4. Create a variable called gets_discount that stores TRUE if the person is a member OR is under 18 (age less than 18)
  5. Create a variable called vip_access that stores TRUE if 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  # TRUE

The OR operator (||) returns TRUE when at least one condition is true:

is_weekend <- TRUE
is_holiday <- FALSE

day_off <- is_weekend || is_holiday  # TRUE

Truth table for logical operators:

ABA && BA || B
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE

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)
quiz iconTest yourself

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

All lessons in Fundamentals