if as an Expression
if returns a value, so there is no ternary operator.
Example
val n = 7
val kind = if (n % 2 == 0) "even" else "odd"
println(kind)
if (n > 5) println("big") else println("small")Exercise
Return from if.
val x = (c) 1 else 2
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.