Conditionals in Kotlin
From the Kotlin cheat sheet · Control Flow · verified Jul 2026
Conditionals
if, when, and conditional expressions
kotlin
// If expression
val max = if (a > b) a else b
// If-else chain
val result = if (score >= 90) {
"A"
} else if (score >= 80) {
"B"
} else if (score >= 70) {
"C"
} else {
"F"
}
// When expression (like switch)
when (x) {
1 -> println("One")
2 -> println("Two")
3, 4 -> println("Three or Four")
in 5..10 -> println("Between 5 and 10")
!in 10..20 -> println("Not between 10 and 20")
is String -> println("It's a string")
else -> println("Unknown")
}
// When as expression
val description = when (val code = getCode()) {
200 -> "OK"
404 -> "Not Found"
500 -> "Server Error"
else -> "Unknown code: $code"
}💡 if and when are expressions that return values
⚡ when without argument acts like if-else chain
📌 Sealed classes with when provide exhaustive checks
🟢 Smart casts work in when branches after type checks