What is the equivalent of switch expression in Kotlin? How does it differ from switch?
Answer Posted / Sachin Kant Siddharth
The equivalent of a switch statement in Kotlin is the when expression. The main difference between them is that the when expression provides more flexibility and can handle multiple cases more concisely.nnFor example:nnval day = 5nnwhen (day) {n 1 -> println("Monday")n 2 -> println("Tuesday")n in 3..6 -> println("It's a weekday")n else -> println("It's the weekend")n}nnIn this example, the when expression can handle out-of-range cases more elegantly and supports the 'in' operator to check if a value falls within a range.
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What are Data classes ? Aren’t they available in Java ?
What is lateinit modifier?
Does Kotlin provide any additional functionalities for standard Java packages or standard Java classes?
What is the type of the following Array?, val arr = arrayOf(1, 2, 3);
How is a function declared? Why are Kotlin functions known as top-level functions?
How do you realize Ternary Conditional Operator in Kotlin ?
Does Kotlin support primitive datatypes as like in Java?
What is the difference between object { } block and companion object { } code block in Kotlin?
What is the difference between == and === operators in Kotlin?
Explain Higher-Order Functions?
What is the latest version of kotlin?