Logical Operators
In this page:
Logical Operators
Logical operators combine Boolean expressions: && (AND), || (OR), and ! (NOT). && and || short-circuit -- if the left side already determines the result, the right side is never evaluated. This matters when the right side has side effects or could throw.
Note: Short-circuiting lets you safely write if (list.nonEmpty && list.head > 0) without risking an error on an empty list.
Example: Logical Operators
object Main extends App {
val isLoggedIn = true
val isAdmin = false
println(isLoggedIn && isAdmin)
println(isLoggedIn || isAdmin)
println(!isAdmin)
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: