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.
| left | right | left || right |
|---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
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 || 2does not mean "x is 1 or 2". The right side of||must be a Boolean expression of its own, and2is anInt, so the compiler rejects it. Writex == 1 || x == 2. - Using a single bar. Kotlin has no single-bar operator, so
a | bis 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 <= 85is 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.
- Write the first comparison. In Kotlin,
!=on strings compares their text, soweather != "sunny"is true for any weather other than exactly"sunny". - Write the second comparison. "At most 85" includes 85 itself, so use
temperature <= 85. - Join them with
||in the condition of anif, and print"Learning to code"inside the block. - Add the
elsebranch, written as} else {on the line that closes theif. It needs no condition. It runs when both comparisons arefalse, 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 || bistruewhen at least one side istrue, andfalseonly when both arefalse.- Each side must be a complete Boolean expression:
x == 1 || x == 2. - Kotlin stops at the left side when it is already
true. - An
elsebranch covers exactly the case where the whole||condition isfalse.