How to use “when” expression in Kotlin programming language?

By Arindam Kundu

openweb kotlin development

How to use “when” expression in Kotlin programming language?

”when” is nothing but ‘Switch’ statement in C and Java language.There is also default case but this is expressed with else in When block.There is no break statement.We can attach multiple cases as well as a range(like 1…20) in one case.Also we can attach multiple condition in one case.The appropriate example is written below-

fun main( args: Array<String>){

// when as expression

val x=2

when (x){

0,1,4 -> println(“x is one”)

2 -> {

println(“x is two”)



3 -> println(“x is three”)

in 5…20 -> println(“x is between 5 to 20”)


println(“x is unknown”)

println(“I don’t know the value of x”)





x is two


Arindam Kundu Subscriber
Android Developer , Openweb Solutions

Android Developer at Openweb Solutions

Posts created 5

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top