Kotlin(五)流程控制

if ... else

传统用法

if(testExpression){

  //如果testExpression为true,则运行代码

}else{

  //如果testExpression为false,则运行代码

}

与Java(和其他许多编程语言)不同,if可以在Kotlin中使用表达式并且返回一个值,例如:

fun main() {

    val number = -10

    val result = if (number > 0) {

        "Positive number"

    } else {

        "Negative number"

    }

    println(result)

}

结果:

Negative number 

如果if分支块包含多个表达式,则最后一个表达式将作为块的值返回,例如:

fun main() {

    val a = -9

    val b = -11

    val max = if (a > b) {

        println("$a is larger than $b.")

        println("max variable holds value of a.")

        a

    } else {

        println("$b is larger than $a.")

        println("max variable holds value of b.")

        b

    }

    println("max = $max")

}

结果:

-9 is larger than -11.

max variable holds value of a.

max = -9

when表达式

fun main() {

    val a = 12

    val b = 5

    println("Enter operator either +, -, * or /")

    val operator = readLine()

    val result = when (operator) {

        "+" -> a + b

        "-" -> a - b

        "*" -> a * b

        "/" -> a / b

        else -> "$operator operator is invalid operator."

    }

    println("result = $result")

}

结果:

Enter operator either +, -, * or /

*

result = 60

when有点像switch..case

使用逗号组合两个或多个分支条件的表达式,例如:

fun main() {

    val n = -1

    when (n) {

        1, 2, 3 -> println("n is a positive integer less than 4.")

        0 -> println("n is zero")

        -1, -2 -> println("n is a negative integer greater than 3.")

    }

}

结果:n is a negative integer greater than 3.

检查范围内的值,例如:

fun main(args: Array) {

    val a = 100

    when (a) {

        in 1..10 -> println("A positive number less than 11.")

        in 10..100 -> println("A positive number between 10 and 100 (inclusive)")

    }

}

检查值是否属于特定类型(is、!is),例如:

when (x) {

    is Int -> print(x + 1)

    is String -> print(x.length + 1)

    is IntArray -> print(x.sum())

}

使用表达式作为分支条件,例如:

fun main(args: Array) {

    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.")

    }

}

结果:Bingo! It's eleven.

Break

first@ for (i in 1..4) {

        second@ for (j in 1..2) {

            println("i = $i; j = $j")

            if (i == 2)

                break@second

        }

    }

break终止标有 @second的循环.

continue 跳过

    for (i in 1..5) {

        println("$i Always printed.")

        if (i > 1 && i < 5) {

            continue

        }

        println("$i Not always printed.")

    }

输出:

1始终打印。

1并不总是打印。

2始终打印。

3始终打印。

4始终打印。

5始终打印。

5并不总是打印。

For循环语法:

for(item in collection){

    //循环体

}

例如:

   print("for (i in 1..5) print(i) = ")

    for (i in 1..5) print(i)

    println()

    print("for (i in 5..1) print(i) = ")

    for (i in 5..1) print(i)            // 无任何输出

    println()

    print("for (i in 5 downTo 1) print(i) = ")

    for (i in 5 downTo 1) print(i)//从5倒序输出到1

    println()

    print("for (i in 1..4 step 2) print(i) = ")

    for (i in 1..5 step 2) print(i)//间隔为2的输出

    println()

    print("for (i in 4 downTo 1 step 2) print(i) = ")

    for (i in 5 downTo 1 step 2) print(i)//间隔为2的倒序输出

    println()

    print("for ((i in 1 until 5) print(i) = ")

    for (i in 1 until 5) print(i)//输出1..4 ,去掉末尾的遍历 

结果:

for (i in 1..5) print(i) = 12345

for (i in 5..1) print(i) =

for (i in 5 downTo 1) print(i) = 54321

for (i in 1..4 step 2) print(i) = 135

for (i in 4 downTo 1 step 2) print(i) = 531

for ( (i in 1 until 5) print(i) =  1234

你可能感兴趣的:(Kotlin(五)流程控制)