Start learning free

LESSON 76 OF 415 · CONDITIONAL STATEMENTS

The And Operator (&&) in Swift

Lesson Read the full explanation

Sometimes you will need to test more than one thing at a time. The logical and operator (&&) returns true if and only if both conditions to the left and right of it are true. For example:

10 == 10 && 7 < 10 // Evaluates to true

We have two conditions separated by && operator:

  • 10 == 10, which evaluates to true
  • 7 < 10, which evaluates to true
As both conditions evaluate to true, the entire line will evaluate to true

An example inside an if statement:

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

Inside the if, we have two conditions separated by && operator:

  • x != 7, equivalent to 10 != 7, which evaluates to true
  • 12 < x, equivalent to 12 < 10, which evaluates to false
As one of the conditions evaluates to false, the entire statement will evaluate to false, and so the program prints nothing.

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


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    SWIFT · CONDITIONAL STATEMENTS

    And Operator in Swift, explained

    What the and operator does

    Sometimes one test isn't enough. The logical and operator, written &&, joins two conditions and gives true only when both of them are true. If either side is false, the whole expression is false.

    10 == 10 && 7 < 10 // true
    

    Both parts are true here: 10 == 10 and 7 < 10. So the combined expression is true.

    LeftRightLeft && Right
    truetruetrue
    truefalsefalse
    falsetruefalse
    falsefalsefalse

    Using it in an if statement

    The combined condition goes into an if, with no parentheses needed:

    let x = 10
    if x != 7 && 12 < x {
        print("This is true!")
    }
    

    The first part, x != 7, is true. The second, 12 < x, is false because 12 is not less than 10. One false side makes the whole condition false, so the block is skipped and the program prints nothing.

    Details that matter in Swift

    • Both sides must be Bool. Each operand is a complete comparison. Writing x == 1 && 2 is an error, because 2 is not a Boolean.
    • Short-circuiting. Swift evaluates the left side first, and when it is false, the right side is never evaluated, since the answer is already known. This makes guards like !items.isEmpty && items[0] > 0 safe: with an empty array, the check on the left fails and the indexing on the right never happens.
    • No chained comparisons. To test that x is between two limits, write 1 < x && x < 10. The form 1 < x < 10 does not compile.
    • The symbol, not the word. Swift has no and keyword. if weather == "sunny" and temperature > 85 fails with expected '{' after 'if' condition.
    • Precedence. && binds tighter than || (or). So true || false && false is true, while (true || false) && false is false. Add parentheses when you mix them.
    • Not. The operator ! flips a Boolean: !(3 > 2) is false.

    The exercise

    The code declares two constants:

    let weather = "rainy"
    let temperature = 90
    

    The program should print Going to the beach only when the weather is "sunny" and the temperature is above 85. In any other case it should print Learning to code.

    1. Write the if. Test the weather with weather == "sunny" and the temperature with temperature > 85, and join them with &&.
    2. Print the beach message inside the braces. It appears only when both checks pass.
    3. Add the else. It prints "Learning to code", so the program still answers when either check is false.
    let weather = "rainy"
    let temperature = 90
    
    if weather == "sunny" && temperature > 85 {
        print("Going to the beach")
    } else {
        print("Learning to code")
    }
    

    The temperature of 90 passes its check, but the weather is "rainy", so the first check fails. The program prints Learning to code. Changing the weather to "sunny" makes both checks true, and it prints Going to the beach. Changing only the temperature to 80 with sunny weather fails the second check and also prints Learning to code.

    Common mistakes

    • Using one &. A single ampersand is a different operator, the bitwise and, and it does not combine two Boolean conditions.
    • Dropping the second comparison. weather == "sunny" && temperature fails to compile, because temperature alone is an Int.
    • Comparing strings with the wrong case. "Sunny" is not equal to "sunny".
    • Using >= by habit. The task says above 85, so a temperature of exactly 85 doesn't qualify.

    Key takeaways

    • a && b is true only when both a and b are true.
    • Each side is a full Boolean expression, and the right side is skipped when the left is false.
    • Use && for ranges and for rules with several requirements, and add parentheses when mixing it with ||.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free