Kotlin Booleans
A Boolean holds one of two values: true or false. Booleans power every decision in a program — every if statement, every loop condition, every permission check. They are the on/off switches of programming.
Declaring a Boolean
val isLoggedIn: Boolean = true
val hasPremium: Boolean = false
val isAdult = age >= 18 // evaluates to true or falseLogical Operators
Logical operators combine or modify Boolean values:
Operator │ Symbol │ Meaning
─────────┼────────┼──────────────────────────────────────
AND │ && │ true only if BOTH sides are true
OR │ || │ true if AT LEAST ONE side is true
NOT │ ! │ flips true to false, false to true
val hasTicket = true
val hasId = false
println(hasTicket && hasId) // false (both needed)
println(hasTicket || hasId) // true (at least one)
println(!hasTicket) // false (flips true)Truth Table: AND (&&)
A │ B │ A && B
───────┼────────┼────────
true │ true │ true
true │ false │ false
false │ true │ false
false │ false │ false
Truth Table: OR (||)
A │ B │ A || B
───────┼────────┼────────
true │ true │ true
true │ false │ true
false │ true │ true
false │ false │ false
Comparison Operators That Return Boolean
val x = 10
val y = 20
println(x == y) // false equal to
println(x != y) // true not equal to
println(x > y) // false greater than
println(x < y) // true less than
println(x >= 10) // true greater than or equal
println(x <= 10) // true less than or equalBoolean in Conditions
val temperature = 38.5
val isFever = temperature > 37.5
if (isFever) {
println("You have a fever. Rest and drink water.")
} else {
println("Temperature is normal.")
}Short-Circuit Evaluation
Kotlin stops evaluating a logical expression as soon as the result is certain. This is called short-circuit evaluation:
For &&:
If the LEFT side is false → skip the right side entirely
Result is already false no matter what
For ||:
If the LEFT side is true → skip the right side entirely
Result is already true no matter what
fun checkAge(): Boolean {
println("Checking age...")
return false
}
fun checkId(): Boolean {
println("Checking ID...")
return true
}
// checkAge() returns false, so checkId() is never called
val result = checkAge() && checkId()
println(result)
// Output:
// Checking age...
// falseBoolean Functions
val flag = true
println(flag.toString()) // "true"
println(flag.and(false)) // false (same as &&)
println(flag.or(false)) // true (same as ||)
println(flag.not()) // false (same as !)Practical Example: Movie Ticket Eligibility
fun main() {
val age = 16
val hasParentalConsent = true
val isAdult = age >= 18
val canWatch = isAdult || hasParentalConsent
println("Age: $age")
println("Is adult: $isAdult")
println("Has parental consent: $hasParentalConsent")
println("Can watch the movie: $canWatch")
}Output:
Age: 16
Is adult: false
Has parental consent: true
Can watch the movie: trueEven though the viewer is not an adult, parental consent makes the combined condition true. The || operator allows either condition to grant access.
