How to implement switch-case in Kotlin? Alternatives and examples
Hi! I've just started learning Kotlin after Java, and I've run into a problem. In Java, I often used switch-case to handle multiple conditions, for example:\n\n```java\nint day = 3;\nString dayName;\nswitch (day) {\n case 1: dayName = "Monday"; break;\n case 2: dayName = "Tuesday"; break;\n case 3: dayName = "Wednesday"; break;\n case 4: dayName = "Thursday"; break;\n case 5: dayName = "Friday"; break;\n default: dayName = "Weekend";\n}\n```\n\nBut when I tried to write the same thing in Kotlin, the compiler gave an error — the `switch` keyword is not defined. I googled it and understood that Kotlin doesn't have a traditional switch-case. Instead, it uses `when`. But I don't fully understand how to use it correctly.\n\nI need:\n\n1. Replace a simple switch-case with integers
No answers yet.
Log in to leave an answer