Give Two or More Options to a when Statement in Kotlin In the switch statement, we can match one case at a time with a given value. But while using when in Kotlin, we can provide multiple options to a single case by separating each with a comma. In the below example, we will print Valid for the numbers 1 and 2.
In Kotlin, we have no switch statement. Instead, Kotlin replaces the switch with the When expression. print("Some other number!")
In Java we use switch but in Kotlin, that switch gets converted to when. when is also used for conditional representation but it does the things in a very smarter and easier way. Whenever you are having a number of possibilities then you can use when in your code.
Compiler generates a jump table for switch case statement. The switch variable/expression is evaluated once. Switch statement looks up the evaluated variable/expression in the jump table and directly decides which code block to execute. If no match is found, then the code under default case is executed.
You could do like this:
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
extracted from official help
switch
in Java is effectively when
in Kotlin. The syntax, however, is different.
when(field){
condition -> println("Single call");
conditionalCall(field) -> {
print("Blocks");
println(" take multiple lines");
}
else -> {
println("default");
}
}
Here's an example of different uses:
// This is used in the example; this could obviously be any enum.
enum class SomeEnum{
A, B, C
}
fun something(x: String, y: Int, z: SomeEnum) : Int{
when(x){
"something" -> {
println("You get the idea")
}
else -> {
println("`else` in Kotlin`when` blocks are `default` in Java `switch` blocks")
}
}
when(y){
1 -> println("This works with pretty much anything too")
2 -> println("When blocks don't technically need the variable either.")
}
when {
x.equals("something", true) -> println("These can also be used as shorter if-statements")
x.equals("else", true) -> println("These call `equals` by default")
}
println("And, like with other blocks, you can add `return` in front to make it return values when conditions are met. ")
return when(z){
SomeEnum.A -> 0
SomeEnum.B -> 1
SomeEnum.C -> 2
}
}
Most of these compile to switch
, except when { ... }
, which compiles to a series of if-statements.
But for most uses, if you use when(field)
, it compiles to a switch(field)
.
However, I do want to point out that switch(5)
with a bunch of branches is just a waste of time. 5 is always 5. If you use switch
, or if-statements, or any other logical operator for that matter, you should use a variable. I'm not sure if the code is just a random example or if that's actual code. I'm pointing this out in case it's the latter.
The switch case is very flexible in kotlin
when(x){
2 -> println("This is 2")
3,4,5,6,7,8 -> println("When x is any number from 3,4,5,6,7,8")
in 9..15 -> println("When x is something from 9 to 15")
//if you want to perform some action
in 20..25 -> {
val action = "Perform some action"
println(action)
}
else -> println("When x does not belong to any of the above case")
}
When Expression
when
replaces the switch operator of C-like languages. In the simplest form it looks like thiswhen (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { // Note the block print("x is neither 1 nor 2") } }
when
matches its argument against all branches sequentially until some branch condition is satisfied.when
can be used either as an expression or as a statement. If it is used as an expression, the value of the satisfied branch becomes the value of the overall expression. If it is used as a statement, the values of individual branches are ignored. (Just like withif
, each branch can be a block, and its value is the value of the last expression in the block.)
From https://kotlinlang.org/docs/reference/control-flow.html#when-expression
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With