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.
| left | right | left || right |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
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.
- Write the
if. The condition is true whenweatheris not"sunny"ortemperatureis at most 85. Each part is its own comparison:weather != "sunny"andtemperature <= 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. - Add the
else. Write it as} else {on the line that closes theif, 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 noorkeyword for booleans. Write||. - Half a comparison on the right.
temperature <= 85 || 90does not mean "at most 85 or at most 90". Each side of||must be a completeBooleanexpression, and90is anInt, 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 || bistruewhen at least one of the two istrue.- Each side is a full
Booleanexpression, and comparisons bind tighter than||. - The right side is skipped when the left side is already
true. - An
elseafter an OR condition covers the case where both sides werefalse.
