switch in Kotlin or “when” in Kotlin.
switch in Kotlin:
If you are familiar with other programming languages, then you might have heard of the term switch statement, which is basically a conditional operator when multiple conditions can be applied on a particular variable. “when” operator matches the variable value against the branch conditions. If it is satisfying the branch condition then it will execute the statement inside that scope. In the following example, we will learn more about “when” in Kotlin.
Example Program:
/* arula.in */
fun main(args : Array<String>)
{
var a : Int = 5
when(a){
1-> print("a == 1")
2-> print("a == 2")
else ->{
print("a is not equals to 1 and 2")
}
}
}
Result :
a is not equals to 1 and 2
No comments