fun main() {
val option = 2
when (option) {
1 -> println("Option is 1")
2 -> println("Option is 2")
3 -> println("Option is 3")
else -> println("Option is neither 1, 2, nor 3")
}
}
- Define a
main
function. - Declare a variable
option
and assign it the value 2
. - Start a
when
expression to check the value of option
. - Check if
option
is equal to 1
, print "Option is 1"
if true. - Check if
option
is equal to 2
, print "Option is 2"
if true. - Check if
option
is equal to 3
, print "Option is 3"
if true. - If none of the above conditions are met, execute the
else
block and print "Option is neither 1, 2, nor 3"
.