Kotlin Loop Control
## For Loop\n\nThe for loop can iterate over anything that provides an iterator. The syntax is:\n\nfor (item in collection) print(item)\nThe loop body can be a code block:\n\nfor (item: Int in ints) { // β¦β¦}\nAs mentioned above, the for loop can iterate over anything that provides an iterator.\n\nIf you want to iterate over an array or a list with indices, you can do it like this:\n\nfor (i in array.indices) { print(array)}\nNote that this "range-based iteration" compiles to an optimized implementation without creating extra objects.\n\nAlternatively, you can use the library function withIndex:\n\nfor ((index, value) in array.withIndex()) { println("the element at $index is $value")}\n### Example\n\nIterating over a collection:\n\nfun main(args: Array) { val items = listOf("apple", "banana", "kiwi") for (item in items) { println(item) } for (index in items.indices) { println("item at $index is ${items}") }}\nOutput:\n\napple banana kiwi item at 0 is apple item at 1 is banana item at 2 is kiwi\n\n* * *\n\n## while and do...while Loops\n\nwhile is the most basic loop. Its structure is:\n\nwhile(Boolean Expression) { //Loop Body}\ndoβ¦while loop: For the while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to execute at least once even if the condition is not met.\n\nThe doβ¦while loop is similar to the while loop, except that the doβ¦while loop executes at least once.\n\ndo { //Code Statement}while(Boolean Expression);\n### Example\n\nfun main(args: Array) { println("----while Use-----") var x = 5 while (x > 0) { println( x--) } println("----do...while Use-----") var y = 5 do { println(y--) } while(y>0)}\nOutput:\n\n----while Use-----54321----do...while Use-----54321\n\n* * *\n\n## Returns and Jumps\n\nKotlin has three structural jump expressions:\n\n* _return_. Returns from the nearest enclosing function or anonymous function by default.\n* _break_. Terminates the nearest enclosing loop.\n* _continue_. Proceeds to the next iteration of the nearest enclosing loop.\n\nIn loops, Kotlin supports the traditional break and continue operators.\n\nfun main(args: Array) { for (i in 1..10) { if (i==3) continue // i Skip current loop and continue to the next iteration (when value is 3) println(i) if (i>5) break // i Break out of the loop (when value is 6) }}\nOutput:\n\n12456\n### Break and Continue Labels\n\nIn Kotlin, any expression can be labeled. The label format is an identifier followed by the @ symbol, for example: abc@, fooBar@ are valid labels. To label an expression, we just put the label in front of it.\n\nloop@ for (i in 1..100) { // β¦β¦}\nNow, we can use labels to restrict break or continue:\n\nloop@ for (i in 1..100) { for (j in 1..100) { if (β¦β¦) break@loop }}\nA label-restricted break jumps to the execution point right after the loop specified by the label. Continue proceeds to the next iteration of the loop specified by the label.\n\n### Label-restricted Returns\n\nKotlin has function literals, local functions, and object expressions. Therefore, Kotlin functions can be nested. Label-restricted returns allow us to return from an outer function. The most important use case is returning from a lambda expression. Recall when we write:\n\nfun foo() { ints.forEach { if (it == 0) return print(it) }}\nThis return expression returns from the nearest enclosing function, which is foo. (Note that this non-local return is only supported for lambda expressions passed to inline functions.) If we need to return from a lambda expression, we must label it and use that label to restrict the return.\n\nfun foo() { ints.forEach lit@ { if (it == 0) return@lit print(it) }}\nNow, it only returns from the lambda expression. Usually, using an implicit label is more convenient. The label has the same name as the function that accepts the lambda.\n\nfun foo() { ints.forEach { if (it == 0) return@forEach print(it) }}\nAlternatively, we can replace the lambda expression with an anonymous function. The return statement inside the anonymous function will return from the anonymous function itself.\n\nfun foo() { ints.forEach(fun(value: Int) { if (value == 0) return print(value) })}\nWhen returning a value, the parser prefers the label-restricted return, i.e.,\n\nreturn@a 1\nmeans "return 1 from label @a", not "return a label-annotated expression (@a 1)".
YouTip