← Back to Scala Course | Chapter 3: Operators & Expressions | Lesson 3 of 7

Logical Operators

Logical operators join yes-or-no questions with words like and, or, and not. They help a program decide based on more than one fact at a time.

In this page:

  1. Logical Operators
Syntax
scala
condition1 && condition2
condition1 || condition2
!condition

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

markup
object Main extends App {
  val isLoggedIn = true
  val isAdmin = false

  println(isLoggedIn && isAdmin)
  println(isLoggedIn || isAdmin)
  println(!isAdmin)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using & and | where && and || are intended, so the right side is always evaluated.
  2. Expecting && and || to evaluate both sides, when they short-circuit and skip the right side if the left decides the result.
  3. Writing !a == b and expecting !(a == b), when ! binds tighter.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.