CS Electrical And Electronics
@cselectricalandelectronics

Code for switch statement in Kotlin?

All QuestionsCategory: KotlinCode for switch statement in Kotlin?
chetan shidling asked 3 years ago

I need short information.

1 Answers
chetan shidling answered 3 years ago

We can write a switch statement in two methods. The code of switch statement for Kotlin is below.
 
fun main(args:Array<String>){
val num : Int = 2

when(num)
{
1 -> println(“It is one”)
2 -> println(“It is two”)
3 -> println(“It is three”)
4 -> println(“It is four”)
else -> println(“Give the proper input”)
}
val num1 : Int = 2

val str = when(num1)
{
1 -> “It is one”
2 -> “It is two”
3 -> “It is three”
4 -> “It is four”
else -> “Give the proper input”
}
println(“Is $str”)
}