When Statement in Kotlin

  •  In Kotlin, when replaces the switch operator of other languages like Java
  • In Kotlin, when can be used in two ways:
  • when as a statement.
  • when as an expression.
When Example:-

fun main(args: Array<String>){  
    var number = 4  
    when(number) {  
        1 -> println("One")  
        2 -> println("Two")  
        3 -> println("Three")  
        4 -> println("Four")  
        5 -> println("Five")  
        else -> println("invalid number")  
    }  
}  

output:-
Four





Comments

Popular posts from this blog

Android Insight Privacy Policy

What is Kotlin ?

What is Realm Database ?