Kotlin when expression
In Kotlin, when replaces the switch operator of other languages like Java. A certain block of code needs to be executed when some condition is fulfilled. The argument of when expression compares with all the branches one by one until some match is found. After the first match is found, it reaches to end of the when block and executes the code next to the when block. Unlike switch cases in java or any other programming language, we do not require a break statement at the end of each case. In Kotlin, when can be used in two ways:
- when as a statement
- when as an expression
Using when as a statement with else
when can be used as a statement with or without else branch. If it is used as a statement, the values of all individual branches are compared sequentially with the argument and execute the corresponding branch where the condition matches. If none of the branches is satisfied with the condition then it will execute the else branch.
Enter any largebody: Sun Sun is a Star Enter any largebody: Mars I don't know anything about it
Using when as a statement without else
We can use when as a statement without else branch. If it is used as a statement, the values of all individual branches are compared sequentially with the argument and execute the corresponding branch where condition matches. If none of the branches are satisfied with the condition then it simply exits the block without printing anything to system output.
Enter name: Mars Process finished with exit code 0
Using when as an expression
If it is used as an expression, the value of the branch with which condition satisfied will be the value of overall expression. As an expression when returns a value with which the argument matches and we can store it in a variable or print directly.
Enter the month number:8 August
If none of the branch conditions are satisfied with the argument, the else branch is executed. As an expression, the else branch is mandatory, unless the compiler can prove that all possible cases are covered with branch conditions. If we cannot use else branch it will give a compiler error.
Error:(6, 17) Kotlin: 'when' expression must be exhaustive, add necessary 'else' branch
Different ways to use when the block in Kotlin
Combine multiple branches in one using comma:
We can use multiple branches in a single one separated by a comma. When common logic is shared by some branches then we can combine them in a single branch. In the example below, we need to check the entered large body is a planet or not, so we combined all the names of planets in a single branch. Anything entered other than planet name will execute the else branch.
Enter name of planet: Earth Planet
Check the input value in the range or not:
Using the in or !in operator, we can check the range of argument passed in when block. ‘in’ operator in Kotlin is used to check the existence of a particular variable or property in a range. If the argument lies in a particular range then in operator returns true and if the argument does not lie in a particular range then !in returns true.
Enter the month number of year: 5 It is summer season Enter the month number of year: 14 Enter valid month of year
Check given variable is of a certain type or not:
Using is or !is operator we can check the type of variable passed as an argument in when block. If the variable is Integer type then is Int returns true else return false.
It is a String
Using when as a replacement for an if-else-if chain:
We can use when as a replacement for if-else-if. If no argument is supplied then the branch conditions are simply boolean expressions, and a branch is executed only when its condition is true:
Check that a string contains a particular prefix or suffix:
We can also check prefix or suffix in a given string by the below method. If the string contains the prefix or suffix then it will return the Boolean value true else return false.
Yes, string started with GeeksforGeeks