What the or operator does
The logical or operator || combines two Bool values into one. The result is true when at least one side is true, and false only when both sides are false.
| Left | Right | Left || Right |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
You use it when either of two reasons is enough to run some code. Each side is usually a comparison:
let x = 10
print(7 >= 10 || 10 < 12) // true: the right side is true
print(x != 10 || 12 < x) // false: both sides are false
Comparison operators such as <, >= and != bind tighter than ||, so no parentheses are needed around each comparison.
Short-circuit evaluation
Swift evaluates || from left to right and stops as soon as the answer is known. If the left side is true, the right side is never evaluated, because the result can't change. This matters when the right side does work or can fail:
func check(_ name: String, _ result: Bool) -> Bool {
print("checked \(name)")
return result
}
if check("left", true) || check("right", true) {
print("done")
}
This prints:
checked left
done
check("right", true) never runs. If the left side had been false, both checks would have printed.
The exercise, step by step
The lesson starts with two constants:
let weather: String = "sunny"
let temperature: Int = 90
- Write the condition. The first branch should run when
weatheris not"sunny"ortemperatureis at most85. That is two comparisons,weather != "sunny"andtemperature <= 85, joined with||. Swift takes no parentheses around the condition of anif, but the braces around the body are required. - Add the else branch. Write
} else {on the line that closes theif. It has no condition of its own: it runs when theifcondition is false, which for an or means both sides were false. Here that is a sunny day above 85.
let weather: String = "sunny"
let temperature: Int = 90
if weather != "sunny" || temperature <= 85 {
print("Learning to code")
} else {
print("Going to the beach")
}
With "sunny" and 90, the left comparison is false (the weather is sunny) and the right one is false (90 is above 85). Both sides are false, so the program prints Going to the beach. Change temperature to 80 and the right side becomes true, so it prints Learning to code.
Or with more than two conditions
You can chain several || operators in one condition. Swift evaluates them left to right and stops at the first true, so the whole condition is true as soon as any single part is. This is handy for checking a value against several allowed options:
let day = "Sat"
if day == "Sat" || day == "Sun" {
print("Weekend")
} else {
print("Weekday")
}
This prints Weekend. Each side is a full comparison of its own, so day appears twice.
Common mistakes
- Using a single bar. In Swift,
|is the bitwise or operator for integers. It doesn't work on two comparisons as a logical or; write||. - Comparing against a whole expression.
weather != "sunny" || "rainy"doesn't compile, because the right side of||must be aBool. Repeat the variable in each comparison:weather == "sunny" || weather == "rainy". - Mixing up the else case. The else branch of an or runs only when every part is false. If you expected it to run when either part is false, you want
&&in the condition instead. - Parentheses around the condition. They are allowed in Swift, but the usual style leaves them out.
Key takeaways
a || bistruewhen at least one side istrue.- The right side is skipped when the left side is already
true. - Both sides must be
Boolexpressions, so repeat the variable in each comparison. - An
elseafter an or condition covers the case where both sides are false.
