Menu
Coddy logo textTech

Logical Operators Part 3

Part of the Fundamentals section of Coddy's Swift journey — lesson 24 of 86.

The OR operator (||) combines two conditions and returns true when at least one condition is true. It only returns false when both conditions are false.

let hasCard = false
let hasCash = true

let canPay = hasCard || hasCash  // true
print(canPay)

In this example, even though the person doesn't have a card, they can still pay because they have cash. The OR operator is perfect when you have multiple acceptable options.

let isWeekend = false
let isHoliday = false

let dayOff = isWeekend || isHoliday  // false

Here, since neither condition is true, dayOff is false.

You can also combine OR with comparisons:

let age = 15
let hasPermission = true

let canWatch = age >= 18 || hasPermission  // true

The OR operator is useful when any one of several conditions being true is enough to proceed — like checking if a user qualifies through different criteria or if any error condition exists.

challenge icon

Challenge

Easy
Write a function canAccessContent that takes isPremiumMember, hasFreeTrial, and hasPromoCode and returns whether a user can access premium content.

A user can access the content if they have at least one of the following: a premium membership, an active free trial, or a valid promo code.

Parameters:

  • isPremiumMember (Bool): Whether the user has a premium membership
  • hasFreeTrial (Bool): Whether the user has an active free trial
  • hasPromoCode (Bool): Whether the user has a valid promo code

Returns: true if the user can access the content through any of the three options, false if none apply (Bool)

Cheat sheet

The OR operator (||) combines two conditions and returns true when at least one condition is true. It only returns false when both conditions are false.

let hasCard = false
let hasCash = true

let canPay = hasCard || hasCash  // true

You can combine OR with comparisons:

let age = 15
let hasPermission = true

let canWatch = age >= 18 || hasPermission  // true

The OR operator is useful when any one of several conditions being true is enough to proceed.

Try it yourself

func canAccessContent(isPremiumMember: Bool, hasFreeTrial: Bool, hasPromoCode: Bool) -> Bool {
    // Write code here
}
quiz iconTest yourself

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

All lessons in Fundamentals