Skip to content
Justice Tech School

Kotlin If as an Expression

if is an expression, so it returns a value and replaces the ternary operator.

Example
val score = 72
val grade = if (score >= 85) "A" else if (score >= 70) "B" else "C"
println(grade)

Exercise

Replace the ternary operator.

val x =  (a > b) a else b

Quiz Test

Is if an expression in Kotlin?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.