fun main() { val x = 3 val result = when (x) { 1, 2 -> "One or Two" 3, 4 -> "Three or Four" else -> "Other" } println(result) }
The variable x is 3, and the when expression checks if x matches any of the values in each branch. The branch 3, 4 -> "Three or Four" matches because x is 3, so it returns "Three or Four".
fun main() { val score = 85 val grade = when (score) { in 90..100 -> "A" in 80..89, 75 -> "B" else -> "C" } println(grade) }
The score is 85, which falls in the range 80..89, so the branch in 80..89, 75 -> "B" matches and returns "B".
fun main() { val y = 5 val output = when (y) { 1, 2 -> "Low" 3, 4 -> "Medium" else -> "High" } println(output) }
The branch 3 4 -> "Medium" is missing a comma between 3 and 4, which causes a syntax error in Kotlin.
fun main() { val input = "apple" val category = when (input) { "banana", "orange" -> "Citrus" "apple", "pear" -> "Pome" else -> "Unknown" } println(category) }
The input is "apple", which matches the branch "apple", "pear" -> "Pome", so the output is "Pome".
In Kotlin, when you separate multiple conditions by commas in a single branch, it means the branch matches if the subject equals any one of those conditions.
