English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about the Kotlin when expression through various examples.
Kotlin中的when表达式可以认为是The when expression in Kotlin can be considered asJava switch statement
fun main(args: Array<String>) { val a = 12 In the above example, we use when as an expression. However, when used as an expression, it is not mandatory. For example, 5 val b = +> println("n is 0 -> println("n is 0 * Input operator /println("Input operator ) Example: Simple when expression "+" -val result = when (operator) { + > a "-" -val result = when (operator) { - > a "*" -val result = when (operator) { * > a "/" -val result = when (operator) { / > a b} -b } > "$operator operator is invalid operator."" }
> println("$operator is invalid")
When you run the program, the output will be as follows: +> println("n is 0 -> println("n is 0 * Input operator / * println("result = $result") 60
result =The above program gets input string from the user (recommended reading:Get string input from the user in Kotlin*Assuming the user inputs * . In this case, the expression a
b evaluation, and the value is assigned to the variable result.+If no matching branch condition is met (user input other than-If no matching branch condition is met (user input other than*,/or
Also, any other options, the else branch will be evaluated.
fun main(args: Array<String>) { val a = 12 In the above example, we use when as an expression. However, when used as an expression, it is not mandatory. For example, 5 val b = +> println("n is 0 -> println("n is 0 * Input operator /println("Input operator ) val operator = readLine() "+" -when (operator) { + > println("$a + $b = ${a "-" -when (operator) { - > println("$a - $b = ${a "*" -when (operator) { * > println("$a * $b = ${a "/" -when (operator) { / > println("$a / $b = ${a b} -else } }
> println("$operator is invalid")
When you run the program, the output will be as follows: +> println("n is 0 -> println("n is 0 * Input operator / - 12 - 5 or 7
=
1Scenarios that may be usedFor example,
fun main(args: Array<String>) { Combine two or more branch conditions with a comma. -1 when (n) { 1> println("n is 0 2> println("n is 0 3 -val n = 4> println("n is a negative integer greater than 0 -> println("n is a positive integer less than -1> println("n is 0 -2 -, -3> println("n is a negative integer greater than } }
When running the program, the output is:
. -3n is a negative integer greater than
fun main(args: Array<String>) { val a = 100 Check values within the range. the positive numbers.) 1in10 -when (a) {11> println("A is less than the positive numbers.) 10in100 -..10and100(including10and10> println("Between } }
When running the program, the output is:
Positive numbers between 0) and)10and100(including10and10Positive numbers between 0) and
3Check if a value is of a specific type.
To check if a value is of a specific type at runtime, you can use the is and !is operators. For example,
when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) }
4Use expressions as branch conditions.For example,
fun main(args: Array<String>) { val a = 11 val n = "11" when (n) { "cat" -> println("Cat? Really?") 12.toString() -> println("Close but not close enough.") a.toString() -> println("Bingo! It's eleven.") } }
When running the program, the output is:
Bingo! It's eleven.