Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Conditional Expression

There is no ternary operator condition ? then : else in Kotlin. Instead, if may be used as an expression:

fun main() {
    fun max(a: Int, b: Int) = if (a > b) a else b         // 1

    println(max(99, -42))
}
  1. if is an expression here: it returns a value.