IF expression
An if statement contains a Boolean expression and one or more statements.
// Traditional Use var max = a if (a < b) max = b // Use else var max: Int if (a > b) { max = a } else { max = b } // As an expression val max = if (a > b) a else b
We can also assign the result of the IF expression to a variable.
val max = if (a > b) { print("Choose a") a } else { print("Choose b") b }
This also shows that I don't need a ternary operator like Java, because we can use it to simply implement:
val c = if (condition) a else b
example
fun main(args: Array<String>) { var x = 0 if(x>0){ println("x Greater than 0") }else if(x==0){ println("x Equal to 0") }else{ println("x Less than 0") } var a = 1 var b = 2 val c = if (a>=b) a else b println("c The value of is $c") }
The output result is:
x Equal to 0 c The value of is 2
Use interval
Use the in operator to detect whether a number is within the specified interval. The interval format is {X y :
example
fun main(args: Array<String>) { val x = 5 val y = 9 if (x in 1..8) { println("x In interval") } }
The output result is:
x In interval
When expression
when compares its parameters with all branch conditions in order until a branch satisfies the condition.
when can be used as either an expression or a statement. If it is treated as an expression, the value of the qualified branch is the value of the whole expression. If it is used as a statement, the value of individual branches is ignored.
when is similar to the switch operator in other languages. Its simplest form is as follows:
when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { // Pay attention to this block print("x Not 1, not 2") } }
In when, else is the same as the default of switch. If none of the other branches satisfy the condition, the else branch is evaluated.
If many branches need to be processed in the same way, you can put multiple branch conditions together and separate them with commas:
when (x) { 0, 1 -> print("x == 0 or x == 1") else -> print("otherwise") }
We can also detect whether a value is in (in) or not in (! In) an interval or set:
when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") }
Another possibility is to detect whether a value is (is) or not (! Is) a specific type of value. Note: due to intelligent transformation, you can access methods and properties of this type without any additional detection.
fun hasPrefix(x: Any) = when(x) { is String -> x.startsWith("prefix") else -> false }
when can also be used to replace the if else if chain. If no parameters are provided, all branch conditions are simple Boolean expressions. when the condition of a branch is true, the branch is executed:
when { x.isOdd() -> print("x is odd") x.isEven() -> print("x is even") else -> print("x is funny") }
example
fun main(args: Array<String>) { var x = 0 when (x) { 0, 1 -> println("x == 0 or x == 1") else -> println("otherwise") } when (x) { 1 -> println("x == 1") 2 -> println("x == 2") else -> { // Pay attention to this block println("x Not 1, not 2") } } when (x) { in 0..10 -> println("x Within this interval") else -> println("x Not within this range") } }
Output results:
x == 0 or x == 1 x Not 1, not 2 x Within this interval
when uses the , in , operator to determine whether an instance is included in the set:
fun main(args: Array<String>) { val items = setOf("apple", "banana", "kiwi") when { "orange" in items -> println("juicy") "apple" in items -> println("apple is fine too") } }
Output results:
apple is fine too