Kotlin Help

Booleans

The 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:

val isTrue: Boolean = true val isFalse = false // Kotlin infers Boolean

If a value can be null, use Boolean?:

val isEnabled: Boolean? = null

Produce Boolean values

You can use comparison expressions and functions to produce Boolean values:

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:

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:

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:

val a = false && false // false val b = false && true // false val c = true && false // false val d = true && true // true

Logical OR

The OR operator returns true if at least one operand is true.

To use logical OR, place the || operator between operands:

val a = false || false // false val b = false || true // true val c = true || false // true val d = true || true // true

Exclusive OR (XOR)

The exclusive OR (XOR) operation returns true if the operands have different values.

To use XOR, write xor between operands:

val a = false xor false // false val b = false xor true // true val c = true xor false // true val d = true xor true // false

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 ||:

fun main() { //sampleStart val result = true || false && false println(result) // true //sampleEnd }

To make evaluation order explicit, use parentheses:

fun main() { //sampleStart val result = (true || false) && false println(result) // false //sampleEnd }

Boolean in conditions

if, when, and while evaluate Boolean expressions to direct program flow.

if expressions

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

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

fun main() { //sampleStart var isCalculating = true while (isCalculating) { println("Calculating...") isCalculating = false } //sampleEnd }
21 April 2026