Lesson 5 of 17

Conditionals

if / else in Kotlin

Kotlin uses if, else if, and else for branching:

if (score >= 90) {
    println("A")
} else if (score >= 80) {
    println("B")
} else {
    println("F")
}

Unlike many languages, if in Kotlin is an expression — it returns a value:

val grade = if (score >= 90) "A" else "B"

Your Turn

Write a function sign(n: Int): String that returns "positive" if n > 0, "negative" if n < 0, and "zero" otherwise.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.