Kotlin learning: Kotlin condition control

Kotlin condition control

if expression

An if statement contains a Boolean expression and one or more statements

// 传统用法
var max = a 
if (a < b) max = b

// 使用 else 
var max: Int
if (a > b) {
    max = a
} else {
    max = b
}
 
// 作为表达式
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

Use interval

Use the in operator to detect whether a number is within a specified interval, the interval format is x..y

fun main(args: Array<String>) {
    val x = 5
    val y = 9
    if (x in 1..8) {
        println("x 在区间内")
    }
}

when expression

when compares its parameters with all branch conditions in order, until a branch meets the condition.

when can be used as an expression or as a statement. If it is used as an expression, the value of the qualified branch is the value of the entire expression. If it is used as a statement, the value of the individual branch 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 -> { // 注意这个块
        print("x 不是 1 ,也不是 2")
    }
}

In when, else is the same as switch's default. If the other branches do not meet the conditions, the else branch will be evaluated.

If many branches need to be handled in the same way, you can put multiple branch conditions together, separated by commas

when (x) {
    0, 1 -> print("x == 0 or x == 1")
    else -> print("otherwise")
}

We can also detect whether a value is (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: Thanks to the smart conversion, you can access the 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 if-else if chains. If no parameters are provided, all branch conditions are simple Boolean expressions, and the branch is executed when the condition of a branch is true

when {
    x.isOdd() -> print("x is odd")
    x.isEven() -> print("x is even")
    else -> print("x is funny")
}

Instance

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 -> { // 注意这个块
            println("x 不是 1 ,也不是 2")
        }
    }

    when (x) {
        in 0..10 -> println("x 在该区间范围内")
        else -> println("x 不在该区间范围内")
    }
}

Output result

x == 0 or x == 1
x 不是 1 ,也不是 2
x 在该区间范围内

The in operator is used in when to determine whether the set contains a certain reality

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 result

apple is fine too

 

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/113481337