Kotlin Boolean

Boolean Basics

Boolean types in Kotlin can be either true or false.

val isActive: Boolean = true
val isVisible = false

Kotlin’s Boolean type is used in conditionals, logic expressions, and validation checks.

Logical Operators

Used to combine or negate boolean expressions:

  • && (AND)
  • || (OR)
  • ! (NOT)
Loading...
Output:

Boolean in Conditionals

val isLoggedIn = true
if (isLoggedIn) {
    println("Welcome back!")
} else {
    println("Please log in.")
}

Boolean Functions

Functions can return Boolean results for conditions or validation:

fun isEven(number: Int): Boolean {
    return number % 2 == 0
}

println(isEven(4))  // true
println(isEven(7))  // false

Boolean Expressions in Assignments

You can assign the result of a comparison directly to a Boolean variable:

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic