Boolean
A Boolean is a light switch that can only be true or false. Programs use it to answer yes-or-no questions.
In this page:
Syntax
val flag: Boolean = true
val result = value1 > value2
Boolean
A Boolean holds either true or false and is the result of every comparison and logical expression. Booleans drive if statements and pattern match guards throughout Scala.
Unlike some languages, Scala never implicitly converts other types to Boolean.
Note:
Comparison operators like
==, >, and < always evaluate to a Boolean value.Example: Boolean
object Main extends App {
val isActive: Boolean = true
val isAdmin: Boolean = false
val isAdult = 20 >= 18
println(s"$isActive $isAdmin $isAdult")
}
Login to try C/C++/Java/PHP code in the editor
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
- Using
=instead of==in a condition, as inif (x = 5), which does not compile. - Writing
TrueorFalsewith a capital letter, when Scala usestrueandfalse. - Using an
Intsuch as1or0as a condition, when Scala does not treat numbers as booleans.
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: