Start learning free

LESSON 79 OF 415 · CONDITIONAL STATEMENTS

Or Operator in Swift: Logical || Explained

Lesson Read the full explanation

Sometimes we will need to run some code if at least one of two conditions is true. The logical or operator (||) returns true if either of the conditions is true. Otherwise, if both conditions are false, it returns false.
For example:

7 >= 10 || 10 < 12 // Evaluates to true

We have two conditions separated by || operator:

  • 7 >= 10, which evaluates to false
  • 10 < 12, which evaluates to true
As at least one of the conditions evaluates to true, the entire line will evaluate to true

Another example:

10 < 15 || 10 > 9 // Evaluates to true as both conditions are true

An example inside an if statement:

let x = 10
if x != 10 || 12 < x { // Evaluates to false
    print("This is true!")
}

Both conditions evaluate to false, so the entire condition evaluates to false and the program prints nothing.

Assignment
Follow the Coding Tutorial and play with the or operator.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    SWIFT · CONDITIONAL STATEMENTS

    Or Operator in Swift, explained

    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.

    LeftRightLeft || Right
    truetruetrue
    truefalsetrue
    falsetruetrue
    falsefalsefalse

    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
    
    1. Write the condition. The first branch should run when weather is not "sunny" or temperature is at most 85. That is two comparisons, weather != "sunny" and temperature <= 85, joined with ||. Swift takes no parentheses around the condition of an if, but the braces around the body are required.
    2. Add the else branch. Write } else { on the line that closes the if. It has no condition of its own: it runs when the if condition 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 a Bool. 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 || b is true when at least one side is true.
    • The right side is skipped when the left side is already true.
    • Both sides must be Bool expressions, so repeat the variable in each comparison.
    • An else after an or condition covers the case where both sides are false.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free