The 'when' expression in Kotlin is a powerful and expressive way to handle conditional logic, making your code cleaner and more readable compared to traditional 'if-else' statements. It allows for multiple conditions to be checked in a concise manner while providing a clear structure for handling different cases.
code
val dayOfWeek = 3
val dayName = when (dayOfWeek) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day"
}
println(dayName) // Output: Wednesday