Updated April 18, 2023
Introduction to Kotlin when
The kotlin when is one of the expression and used it on the conditional statements which has returned the value it has the replacement of the other feature called a switch statement. “when” keyword used normally it is not mandatory also the when the statement is used in the multiple branches of the condition. It can be separated from the comma delimiters because it needs to run and use the same logic implemented by the programmers for multiple choices. It has used with the expression also checks the input ranges; it provided with user input condition that range is considered with the operators.
Syntax
In kotlin language, it has many default keywords, variables, and functions to implement the application. One of the default keywords can be used in both expression and non-expression scenarios.
fun main(args: Array<String>){
val variablename;
when(variablename)
{
---some coding logics it depends upon the requirement---
}
}
The above codes are the basic syntax for utilizing the when keyword on the kotlin codes. We want to write the expression based on the function’s requirement on the child function, or the inheritance concept will be implemented on the application.
Working of when statement in Kotlin
Generally, “when” is one of the keywords, and it is used in both expression and non-expression; it also checks and combined with the valid conditional statements depending on the calculation based on the user requirements, in kotlin, when it is constructed, and it can be thought of as the replacement for the switch case statement, which is similar to the other languages when the keyword is used as the expression and validating the condition like matching the values for overall expression. If condition statement, the values of the individual branches are always ignored, each branch can be of the block, and its value is the last expression of the block values.
Else loop is evaluated if none of the other branches and the condition is satisfied with the boolean condition statements; else, a loop is mandatory unless the compiler can prove that all possible cases are covered with the loop conditions. The scope of the variable is introduced with the keyword for when the subject is restricted to the body of this keyword for when expression. We can check and validate the condition using a particular type in both compile and runtime in the kotlin operators. With the help of conditional statements, the loop will use the control, and it executes the other conditions based on their needs.
Examples of Kotlin when
Below are the examples of Kotlin when:
Example #1
Code:
package one;
fun main(args: Array<String>){
println("Welcome To My Domain it’s a first example regarding the When keyword using the kotlin program logics")
var first = 7
var second = when(first) {
1 -> "It’s a first user input value"
2 -> "It’s a second user input value"
3 -> "It’s a third user input value"
4 -> "It’s a four user input value"
5 -> "It’s a fifth user input value"
6 -> "It’s a sixth user input value"
7 -> "It’s a seventh user input value"
8 -> "It’s a eight user input value"
9 -> "It’s a ninth user input value"
10 -> "It’s a tenth user input value"
else -> "Please try again"
}
println("$second")
var third = when(second) {
"Eleven" -> "It’s an eleventh user input value"
"Twelve" -> "It’s a twelfth user input value"
"Thirteen" -> "It’s a thirteen user input value"
"Fourteen" -> "It’s a fourteenth user input value"
"Fifteen" -> "It’s a fifteenth user input value"
"Sixteen" -> "It’s a sixteenth user input value"
"Seventeen" -> "It’s a seventeenth user input value"
"Eighteen" -> "It’s a eighteenth user input value"
"Nineteen" -> "It’s a nineteenth user input value"
"Twenty" -> "It’s a twenty user input value"
else -> "Please try again"
}
println("Thank you users have a nice day please find your outputs $first")
}
Output:
We used the above example when keywords in basic formats like int, string, string, and string formats. We can print the statements on the user console screen.
Example #2
Code:
package one;
fun main(args: Array<String>){
println("Welcome To My Domain it’s a second example regarding the When keyword using the kotlin program logics")
var first = 'A'
when(first){
'A', 'E', 'I', 'O', 'U' -> println("$first is a Vowel characters")
else -> println("$first is a Consonant characters")
}
println("Thank you users have a nice day kindly try again please keep and stay with our application $first")
var yrsage = 16
when(yrsage) {
in 18..100 -> {
val numlast = 100 - yrsage
println("Your marraige age is in $numlast years")
}
in 18..100 -> println("Thank you users for spending the time with our application please keep and stay with this.")
}
}
Output:
In the second example, we used to calculate the vowels like a,e, i,o,u and calculate the marriage in years format.
Example #3
Code:
package one;
enum class Third(val exampl: Boolean = false){
January(true),
February,
March,
April(true),
May,
June,
July(true),
August,
September,
October,
November,
December(true);
companion object{
fun demo(obj: Third): Boolean {
return obj.name.compareTo("April") == 0 || obj.name.compareTo("December") == 0
}
}
}
fun demos(th: Third) {
when(th) {
Third.January -> println("January")
Third.February->println("February")
Third.March -> println("March")
Third.April -> println("April")
Third.May -> println("May")
Third.June -> println("June")
Third.July ->println("July")
Third.August ->println("August")
Third.September ->println("September")
Third.October ->println("October")
Third.November ->println("November")
Third.December -> println("December")
}
}
fun main(){
println("Welcome To My Domain its a third example regarding the When keyword using the kotlin program logics")
for(eg in Third.values()) {
println("${eg.ordinal} = ${eg.name} and is months in ${eg.exampl}")
}
val demo1 = Third.April;
println("Thank you users your current month is ${Third.demo(demo1)}")
}
Output:
In the final example, we used to calculate the current month status by using the boolean condition. We used the when keyword to print the enum class value on the function.
Conclusion
In the conclusion part, kotlin when is one of the conditional statements like if, else, etc. The when keyword supports non -conditional expressions also instead of the switch case statement, these statements will execute the user inputs on each step. It supports all types of user browsers, so it’s compatible when keyword satisfied with some range intervals, which depends upon the requirement.
Recommended Articles
This is a guide to Kotlin when. Here we discuss the introduction, syntax, and working of when statement in Kotlin along with the examples and outputs. You may also have a look at the following articles to learn more –