Start learning free

LESSON 76 OF 414 · CONDITIONAL STATEMENTS

And Operator in Scala: Combining Conditions with &&

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:

var x = 10
if(x != 7 && 12 < x) { // Evaluates to false
    println("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

    SCALA · CONDITIONAL STATEMENTS

    And Operator in Scala, explained

    Testing more than one thing

    Sometimes a single comparison isn't enough. You may want to go to the beach only if it is sunny and hot. The logical and operator && joins two Boolean conditions and gives true only when both are true.

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

    Here 10 == 10 is true and 7 < 10 is true, so the whole expression is true. If either side were false, the result would be false.

    LeftRightLeft && Right
    truetruetrue
    truefalsefalse
    falsetruefalse
    falsefalsefalse

    Inside an if statement

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

    With x equal to 10, x != 7 is true but 12 < x is false. One false side makes the whole condition false, so the block is skipped and nothing is printed.

    Scala details

    • Comparisons bind tighter than &&. In weather == "sunny" && temperature > 85, each comparison is evaluated first and then the results are combined. You don't need extra parentheses, though they never hurt.
    • It short-circuits. If the left side is false, Scala doesn't evaluate the right side at all, since the answer is already known. A call on the right with a side effect, such as a print, doesn't run when the left is false.
    • Both sides must be Boolean. Writing x && 5 with an Int doesn't compile.
    • &&, not and. Scala has no keyword and. A single & exists too, but it is the non-short-circuit form for Booleans and bitwise and for integers, so use && in conditions.
    • String equality uses ==. In Scala, == compares the text of two strings, so weather == "sunny" does what it appears to do.

    and versus nested ifs

    Two conditions that must both hold can also be written as one if inside another. That is longer, and the else case then has to be written twice:

    if (weather == "sunny") {
        if (temperature > 85) {
            println("Going to the beach")
        }
    }
    

    The version with && says the same thing in one line, and a single else then covers every case in which either check fails. Prefer && when both checks lead to the same outcome.

    The exercise

    The starter code declares two values:

    val weather = "rainy"
    val temperature = 90
    

    The task has two steps.

    1. Join both checks with &&. The weather must equal "sunny" and the temperature must be greater than 85. Print "Going to the beach" inside the if, so it appears only when both checks pass.
    2. Add an else. It prints "Learning to code", so the program still responds when either check fails.
    val weather = "rainy"
    val temperature = 90
    
    if (weather == "sunny" && temperature > 85) {
        println("Going to the beach")
    } else {
        println("Learning to code")
    }
    

    The temperature of 90 passes its check, but "rainy" == "sunny" is false. The whole condition is false and the program prints Learning to code. If you change weather to "sunny", both checks pass, and it prints Going to the beach. A sunny day with a temperature of 85 also prints Learning to code, because 85 is not greater than 85.

    Common mistakes

    • A single &. It compiles for Booleans but doesn't short-circuit. Use &&.
    • Writing a shortcut such as weather == "sunny" && > 85. Each side of && must be a complete condition, so repeat temperature.
    • Misjudging the boundary. > 85 excludes 85 itself. Use >= when the value should count.
    • Different capitalization. "Sunny" is not equal to "sunny".

    Key takeaways

    • a && b is true only when both a and b are true.
    • Each side must be a complete Boolean expression.
    • The right side isn't evaluated when the left side is false.
    • An else covers every case where at least one check fails.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free