How to implement switch-case in Kotlin: analogs and examples
Hello, colleagues! I just started learning Kotlin after Java and ran into a problem: Kotlin doesn't have the familiar switch-case operator. In Java, I often used it to handle different values of a single variable, for example, to select an action depending on the day of the week or order status.
Here's how I wrote it in Java:
```java
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Unknown day";
}
```
When I tried to write the same thing in Kotlin, the compiler gave an error: "Unresolved reference: switch". I googled it and found out that in Kotlin, when is used instead of switch-case. But I
No answers yet.
Log in to leave an answer
Blogs