Introduction to Kotlin When Multiple Conditions
Kotlin when multiple conditions replace the switch like java language. The specified code block needs to be executed when we have fulfilled the specified condition. The expression argument is compared with all branches one by one until we find the match in an expression. After finding the first match, it will reach the end of the block and execute the next code.
Key Takeaways
- When we contain the multiple conditions of the branch by using constants is handled by using when multiple conditions in kotlin.
- We can test the conditions against ranges by using multiple conditions in kotlin. While using order, the order of expression is very important to define the example.
Overview of Kotlin When Multiple Conditions
Using kotlin in multiple conditions will execute the code into the when block. The switch cases we are using in java or other languages, in that we have not required break statement at the ending of any case. In kotlin, we can use when multiple conditions by using two ways: when as a statement and when as an expression. Multiple conditions in kotlin will take the value as an input and check the values against the condition we have provided when multiple conditions are important in kotlin.
When to Use Kotlin Conditional Expressions?
The conditional expression is similar to the switch statement we are using in java. The when the expression will match the argument against all the branches. The kotlin when multiple conditions are easier to maintain than the nested if statement. It will improve the code readability by reducing the repetitive code. The when statement in kotlin expresses the code logic better. We are using when conditional expression for executing different types of code depending on the values of the specified expression.
The below example shows how we are using kotlin conditional expression. The below example p is 15, specifying the statement’s first branch and will print x == 15. Similar to the if statement, the branch contains the block enclosed by parenthesis.
Code:
fun main() {
val p = 15
when (p)
{
15 -> print ("p == 15")
25 -> print ("p == 25")
else -> {
print ("p is 15 or 25")
}
}
}
Output:
The kotlin when the conditional expression is a modified version of the switch statement by specifying the return value. In the below code, we have handled the multiple conditions by using the when condition as follows.
Code:
fun main(args : Array<String>) {
val mon = "Sunday"
val month = when(mon) {
"Jan", "Feb", "Mar" -> "First Quarter"
"Apr", "May", "June" -> "Second half"
else -> "Half quarter end"
}
Println ("mon is $month")
}
Output:
The below example shows when expression by using condition as follows. In the below example, we are using the when condition expression as follows.
Code:
fun main(args : Array<String>)
{
val let1 = "P"
val let2 = "p"
when (let1) {
let2.toUpperCase () ->{
println (let2)
}
else -> {
println ("Not equal")
}
}
}
Output:
Kotlin Multiple Conditions Function
In kotlin, when is defining the conditional expression by using the multiple branches. When matches the argument against the branches until the branch condition is satisfied. We can use when by using an expression, or also we can use by using a statement. We can use the same by using statements that can ignore an individual branch’s values. The below example shows the multiple condition function in kotlin as follows.
Code:
fun main(args : Array<String>) {
val p = 1
when (p) {
1 -> print("p == 1")
2 -> print("p == 2")
else -> {
print("p is 1 or 2")
} }
}
Output:
We can also use the in-operator function with the kotlin multiple condition functions. In the example below, we are using an operator inside the when block to check whether the specified value is present or not in a range.
Code:
fun main(args : Array<String>) {
val stud_age = 6
val stat = when(stud_age) {
0 -> "Student"
in 5..6 -> "1st Std"
in 6..7 -> "2nd Std"
in 7..8 -> "3rd Std"
in 9..10 -> "4th Std"
else -> "Stud age is small"
}
println (stat);
}
Output:
Examples of Kotlin When Expression
Given below are the examples mentioned.
Example #1
The below example shows kotlin when expressing. We can use the kotlin when expressing the first branch value for the overall expression.
Code:
fun main() {
val p = 1
val sq = when (p) {
1 -> 1
2 -> 4
3 -> 9
else -> 0
}
println (sq)
}
Output:
Example #2
In the below example, we are using when as an expression, so it will return the value as follows. In the below example, we are providing the integer numbers as follows.
Code:
fun main()
{
val num = 10
val number = when (num)
{
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 5
6 -> 6
7 -> 7
else -> "number not matching"
}
println (number)
}
Output:
Example #3
In the example below, we are using when in expression, we include the condition of even and add as follows.
Code:
fun main()
{
val num = 0
val res = when (num)
{
1 -> "odd"
2 -> "even"
else -> "number invalid"
}
println (res)
}
Output:
FAQ
Given below are the FAQ mentioned:
Q1. How to use when multiple conditions without argument in kotlin?
Answer:
We need to use the if-else condition to use the when multiple conditions without using any argument. We are using the first branch to choose the matches.
Q2. What is when expression in kotlin?
Answer:
When is defining the conditional expression in kotlin. When the condition matches the argument against all the branches, we can use when either as a statement or expression. We can use the same as a statement.
Q3. What is the difference between when statement and an expression?
Answer:
We use when statements and expressions. We are using the when statement with the else statement, or we can also use the when statement without the else statement.
Conclusion
The switch cases we are using in java or other languages, in that we have not required break statement at the ending of any case. The expression argument is compared with all branches one by one until we find the match in an expression.
Recommended Articles
This is a guide to Kotlin When Multiple Conditions. Here we discuss the introduction, when to use kotlin conditional expressions, condition function & examples. You may also have a look at the following articles to learn more –