Created
June 12, 2023 11:21
-
-
Save kalaiselvan369/80796594f5a21f50ebaa6b31593e4431 to your computer and use it in GitHub Desktop.
Usage of when
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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