Skip to content

Instantly share code, notes, and snippets.

@kalaiselvan369
Created June 12, 2023 11:21
Show Gist options
  • Save kalaiselvan369/80796594f5a21f50ebaa6b31593e4431 to your computer and use it in GitHub Desktop.
Save kalaiselvan369/80796594f5a21f50ebaa6b31593e4431 to your computer and use it in GitHub Desktop.
Usage of when
package using_when
import kotlin.random.Random
fun main() {
val a = Random.nextInt(2)
when (a) {
2 -> println("even")
1 -> println("odd")
// else not mandatory since when doesn't return any result
}
val b = Random.nextInt(3)
val result = when(b) {
3 -> 3 + 3
2 -> 2 + 2
1 -> 1 + 1
else -> 0
}
println(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment