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 // falseHere, 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 // trueThe 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
EasyWrite 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 membershiphasFreeTrial(Bool): Whether the user has an active free trialhasPromoCode(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 // trueYou can combine OR with comparisons:
let age = 15
let hasPermission = true
let canWatch = age >= 18 || hasPermission // trueThe 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
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input