Start learning free

LESSON 79 OF 414 · CONDITIONAL STATEMENTS

Logical OR Operator (||) in Scala

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:

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

Both conditions evalute 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

    SCALA · CONDITIONAL STATEMENTS

    Or Operator in Scala, explained

    What the OR operator does

    The logical OR operator, written ||, joins two Boolean conditions. The result is true when at least one side is true, and false only when both sides are false. Use it when any one of several reasons is enough to run some code.

    leftrightleft || right
    truetruetrue
    truefalsetrue
    falsetruetrue
    falsefalsefalse

    Examples

    7 >= 10 || 10 < 12   // false || true  gives true
    10 < 15 || 10 > 9    // true  || true  gives true
    

    Each side of || is a complete comparison. The comparison operators bind tighter than ||, so 7 >= 10 || 10 < 12 is read as (7 >= 10) || (10 < 12) and needs no extra parentheses.

    Inside an if, the condition goes in parentheses:

    val x = 10
    if (x != 10 || 12 < x) {
        println("This is true!")
    }
    

    Here x != 10 is false and 12 < x is false, so the whole condition is false and the program prints nothing. A val works as well as a var here, because the code only reads x.

    Short-circuit evaluation

    Scala 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 no longer change. This matters when the right side is expensive or could fail:

    def noisy(name: String, v: Boolean): Boolean = { println("checking " + name); v }
    
    println(noisy("a", true) || noisy("b", true))
    
    checking a
    true
    

    Only "checking a" appears: the call for "b" never runs. The single-character form | also works on booleans, but it evaluates both sides, so it is rarely what you want in a condition.

    The lesson's exercise

    The code starts with two values:

    val weather = "sunny"
    val temperature = 90
    

    The task has two steps.

    1. Write the if. The condition is true when weather is not "sunny" or temperature is at most 85. Each part is its own comparison: weather != "sunny" and temperature <= 85. Join them with || and print "Learning to code" in the block. In Scala, == and != on strings compare the text, so these comparisons behave as they read.
    2. Add the else. Write it as } else { on the line that closes the if, and print "Going to the beach". The branch has no condition: it runs when both sides of the OR were false, which means the weather is "sunny" and the temperature is above 85.

    The finished program:

    val weather = "sunny"
    val temperature = 90
    
    if (weather != "sunny" || temperature <= 85) {
        println("Learning to code")
    } else {
        println("Going to the beach")
    }
    

    With the starting values, weather != "sunny" is false and temperature <= 85 is false (90 is above 85), so the else runs and the program prints Going to the beach. Changing temperature to 80 makes the second comparison true, and the program prints Learning to code.

    Common mistakes

    • Using the word or. Scala has no or keyword for booleans. Write ||.
    • Half a comparison on the right. temperature <= 85 || 90 does not mean "at most 85 or at most 90". Each side of || must be a complete Boolean expression, and 90 is an Int, so the compiler rejects it.
    • Mixing up != and ==. The exercise wants the weather to be not sunny, which is !=.
    • Expecting both sides to be true. That is AND (&&). OR is satisfied by a single true side.

    Key takeaways

    • a || b is true when at least one of the two is true.
    • Each side is a full Boolean expression, and comparisons bind tighter than ||.
    • The right side is skipped when the left side is already true.
    • An else after an OR condition covers the case where both sides were false.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free