October 2018
Intermediate to advanced
370 pages
9h 15m
English
Similarly to the if statement, when can also be used as an expression. To do this, create a variable and assign when as an expression as follows:
fun main(args: Array<String>) { val grade = "A" val remarks = when (grade) { "A","a" -> "Excellent" "B","b" -> "Very Good" "C","c" -> "Nice work" "D","d" -> "OK" "E","e" -> "Hmmm" "F","f" -> "Better luck next time" else -> "Invalid input" } println(remarks) }
Read now
Unlock full access