BeginnerControl Flow
When Expression
Pattern matching with when.
kotlin
fun main() {
val day = 3
val dayName = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
else -> "Weekend"
}
println(dayName)
}Output
Wednesday
Explanation
when matches the value and returns the corresponding result. else is the default case.