Start learning free

LESSON 77 OF 348 · CONDITIONAL STATEMENTS

Or Operator in Kotlin: 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:

val 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

    KOTLIN · CONDITIONAL STATEMENTS

    Or Operator in Kotlin, explained

    What the or operator does

    Sometimes a piece of code should run when at least one of two conditions holds. The logical or operator, ||, joins two Boolean expressions and gives true if either side is true. It gives false only when both sides are false.

    leftrightleft || right
    truetruetrue
    truefalsetrue
    falsetruetrue
    falsefalsefalse

    Each side is usually a comparison. In 7 >= 10 || 10 < 12, the left side is false and the right side is true, so the whole expression is true.

    Using || in Kotlin

    An || expression is a normal Boolean value. You can print it, store it in a val, or use it as the condition of an if:

    val x = 10
    println(x != 10 || 12 < x)   // false: both sides are false
    println(x == 10 || 12 < x)   // true: the left side is true
    
    if (x != 10 || 12 < x) {
        println("At least one side is true")
    }
    

    This prints false and then true. The if block does not run, because both comparisons are false and the program prints nothing for it.

    Short-circuit evaluation

    Kotlin evaluates the left side first. If it is already true, the answer can't change, so Kotlin skips the right side entirely. This is called short-circuiting, and it matters when the right side has an effect such as printing or a call that can fail:

    fun check(label: String, result: Boolean): Boolean {
        println("checking $label")
        return result
    }
    
    fun main() {
        println(check("a", true) || check("b", false))
    }
    

    The output is:

    checking a
    true
    

    check("b", false) never runs. If the left side is false, Kotlin does evaluate the right side, because it decides the result.

    The word or

    Kotlin also has an infix function named or on Boolean, so a or b is legal. It evaluates both sides every time, with no short-circuiting. In the example above, check("e", false) or check("f", true) prints both labels. In conditions, || is the operator to use.

    Mixing || with &&

    When a condition has both operators, && is evaluated before ||, in the same way that multiplication comes before addition. With a = false, b = true and c = true, the expression a && b || c is read as (a && b) || c and gives true. Written as a && (b || c), the same values give false. Parentheses make your intention visible, so use them whenever both operators appear together.

    Common mistakes

    • Writing the comparison only once. x == 1 || 2 does not mean "x is 1 or 2". The right side of || must be a Boolean expression of its own, and 2 is an Int, so the compiler rejects it. Write x == 1 || x == 2.
    • Using a single bar. Kotlin has no single-bar operator, so a | b is a syntax error. Use || between conditions.
    • Confusing or with and. || needs only one true side, while && needs both. If a condition seems to let too much through, check which one you wrote.
    • Negating one side only. weather != "sunny" || temperature <= 85 is not the same as !(weather == "sunny" || temperature <= 85). The second one is true only when both original comparisons are false. Add parentheses so the grouping is clear.

    The lesson's exercise

    The code already declares two values: weather is "sunny" and temperature is 90. The task is to print "Learning to code" when the weather is not sunny or the temperature is at most 85, and to print "Going to the beach" otherwise.

    1. Write the first comparison. In Kotlin, != on strings compares their text, so weather != "sunny" is true for any weather other than exactly "sunny".
    2. Write the second comparison. "At most 85" includes 85 itself, so use temperature <= 85.
    3. Join them with || in the condition of an if, and print "Learning to code" inside the block.
    4. Add the else branch, written as } else { on the line that closes the if. It needs no condition. It runs when both comparisons are false, which means the weather is sunny and the temperature is above 85, and it prints "Going to the beach".
    val weather = "sunny"
    val temperature = 90
    
    if (weather != "sunny" || temperature <= 85) {
        println("Learning to code")
    } else {
        println("Going to the beach")
    }
    

    With these values, weather != "sunny" is false and temperature <= 85 is false, so the program prints Going to the beach. Change temperature to 80 and the second comparison becomes true, so it prints Learning to code. Change weather to "rainy" with the temperature still at 90, and the first comparison alone is enough.

    Key takeaways

    • a || b is true when at least one side is true, and false only when both are false.
    • Each side must be a complete Boolean expression: x == 1 || x == 2.
    • Kotlin stops at the left side when it is already true.
    • An else branch covers exactly the case where the whole || condition is false.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free