Control flow if expression: In Kotlin, if is an expression that can return a value. Is it except condition? then : Var Max = 1 if (Max < 2){Max = 2}else{Max = 3} var max2 = if (2 > 1) 2 else 1 Val max3 = if (2 > 1){println("2") a}else{println("1") 1} when: val max3 = if (2 > 1){println("2") a}else{println("1") 1} When checks all branches until one condition is met. When can be used as an expression or declaration. If used as an expression, the branch that satisfies the condition is the total expression. If used as a declaration, the value of the branch is ignored. (Like if expressions, each branch is a block of statements, When (1111){1 -> println("1") 2 -> println("2") else -> println("none")} when(2222){1,2 -> print("1 or 2") else -> println("none")} when (3333) {parseInt(333) -> print("s encode x") else -> print("s does not encode x")} In checks if values are in a set: when (100) {in 1.. 100 -> print("0 - 100") ! in 10.. 20 -> print(" 10-20 ") else -> print("none")} // When can also be used instead of if-else if If no arguments are provided, the condition for the branch is a simple Boolean expression that executes the corresponding branch when the condition is true: Fun dd(a:Int,b:Int){when{a < b -> println("a < b") a > b -> println("a > b") else -> println("a = b")}} Fun main(args: Array<String>) {/** * for */ for (I in 1.. 20){if (I % 2 == 0){continue} println(I)} /** * while loop and do... While */ var number = 1 var times = 0 var total = 0 while (total < 15050){total += number number += 1 times += 1} Println ("$number = 15050 ") $times") do {total += number number += 1 times += 1}while (total<15050)} Array<String>) {val a = 5 val b = 10 val result = if (a > b) ">" else b - a when (result) {in 1.. 5 - > {println (" between 1 to 5)} 1,3,5 - > {println (" 1,3,5 ")} (9-6) - > {println (" value is 3 ")} is Int - > {println (" value is of type Int ")} the else -> {println(" value is String ")}}} Return and jump: Kotlin has three types of jump operators: return break to end the most recent closed loop continue to jump to the next loop of the most recent closed loop fun main(args: Array<String>) {// Break and continue tags // In Kotlin expressions can add labels. Tags are denoted by an @ ending, such as ABC @ and fooBar@ are valid (see syntax). // Now we can use tags to make quick breaks or continue: loop @for (I in 1.. 100) { for (j in i.. 100) {if (true) break@loop}} // Return tags in literal functions, local functions, and object expressions. Functions can be wrapped in Kotlin. Return allows us to return to the outer function. The most important example is returning from a literal function, remember how we wrote it before: Var ints = mutableListOf<Int>(1,2,3,4,5,6,7) fun foo() {ints.foreach {if (it == 0) return print(it)}} //return The expression returns to the nearest closed function, such as foo (note that such nonlocal returns can only be used in inline functions). If we need to return from a literal function we can use the tag to modify the return: Fun foo2() {ints.foreach Lin @{if (it == 0) return@lin println(it)}} // Now it just returns from the literal function. Always use a more convenient, implicit label: for example, a label with the same name as the lambda expression passed in. Fun foo3() {ints.foreach {if (it == 0) return@forEach print(it)}} Using a return statement in a function expression returns from the function expression. fun foo4() { ints.forEach(fun(value: Int){ if (value == 0) return print(value) }) }}Copy the code