When as an expression

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)
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.71.94