# Booleans The [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/) type represents logical values: `true` and `false`. Use `Boolean` values in functions that answer yes-or-no questions, and in the `while`, `if`, and `when` conditions. ## Declare a `Boolean` variable To declare a `Boolean` variable, assign it `true` or `false`. You can specify the `Boolean` type explicitly or let Kotlin infer it from the value: ```KOTLIN val isTrue: Boolean = true val isFalse = false // Kotlin infers Boolean ``` If a value can be `null`, use `Boolean?`: ```KOTLIN val isEnabled: Boolean? = null ``` Note: You cannot assign an integer value to a `Boolean` variable. In Kotlin, `0` and `1` are not `Boolean` values. ## Produce `Boolean` values You can use comparison expressions and functions to produce `Boolean` values: ```KOTLIN fun main() { //sampleStart val number = 10 val isPositive = number > 0 println(isPositive) // true val language = "Kotlin" val isEmpty = language.isEmpty() println(isEmpty) // false //sampleEnd } ``` You can use the results in conditions and other expressions as well: ```KOTLIN fun main() { //sampleStart val number = 10 val isPositive = number > 0 // true if (isPositive) { println("The number is positive.") } //sampleEnd } ``` ## `Boolean` operations Kotlin provides operators and infix functions for working with `Boolean` values. You can use them to invert a `Boolean` value or combine multiple `Boolean` values into a single result. ### Negation (NOT) The NOT operator inverts a `Boolean` value. To use NOT, place the `!` operator before a `Boolean` value: ```KOTLIN val isOn = true val isOff = !isOn // isOff is false ``` ### Logical AND The AND operator returns `true` only if both operands are `true`. To use logical AND, place the `&&` operator between operands: ```KOTLIN val a = false && false // false val b = false && true // false val c = true && false // false val d = true && true // true ``` Note: If the first operand is `false`, the `&&` operator skips the second operand. To evaluate both operands, use the `and` [infix function](functions.html#infix-notation) instead. ### Logical OR The OR operator returns `true` if at least one operand is `true`. To use logical OR, place the `||` operator between operands: ```KOTLIN val a = false || false // false val b = false || true // true val c = true || false // true val d = true || true // true ``` Note: If the first operand is `true`, the `||` operator skips the second operand. To evaluate both operands, use the `or` [infix function](functions.html#infix-notation) instead. ### Exclusive OR (XOR) The exclusive OR (XOR) operation returns `true` if the operands have different values. To use XOR, write `xor` between operands: ```KOTLIN val a = false xor false // false val b = false xor true // true val c = true xor false // true val d = true xor true // false ``` Note: `xor` is an [infix function](functions.html#infix-notation), not an operator. Learn more about `Boolean` functions in the [API Reference](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/). ## Operator precedence If an expression contains multiple logical operations and no parentheses to specify the evaluation order, Kotlin applies precedence rules. Operations with higher precedence are evaluated before operations with lower precedence. For the `Boolean` operations described in this section, the precedence order is as follows: 1. `!` 2. `xor` (and other infix functions) 3. `&&` 4. `||` In the following example, the compiler evaluates `&&` before `||`: ```KOTLIN fun main() { //sampleStart val result = true || false && false println(result) // true //sampleEnd } ``` To make evaluation order explicit, use parentheses: ```KOTLIN fun main() { //sampleStart val result = (true || false) && false println(result) // false //sampleEnd } ``` ## `Boolean` in conditions [if](control-flow.html#if-expression), [when](control-flow.html#when-expressions-and-statements), and [while](control-flow.html#while-loops) evaluate `Boolean` expressions to direct program flow. ### `if` expressions ```KOTLIN fun main() { //sampleStart val number = 4 val isEven = number % 2 == 0 // Condition already has the `Boolean` type // You do not need to compare it to `true` or `false` if (isEven) { println("The number is even.") } else { println("The number is odd.") } //sampleEnd } ``` ### `when` expressions ```KOTLIN fun main() { //sampleStart val number = 3 when { number > 0 -> println("The number is positive.") number < 0 -> println("The number is negative.") else -> println("The number is zero.") } //sampleEnd } ``` ### `while` loops ```KOTLIN fun main() { //sampleStart var isCalculating = true while (isCalculating) { println("Calculating...") isCalculating = false } //sampleEnd } ```